English 中文(简体)
Shared memory vs. Go channel communication
原标题:

One of Go s slogans is Do not communicate by sharing memory; instead, share memory by communicating.

I am wondering whether Go allows two different Go-compiled binaries running on the same machine to communicate with one another (i.e. client-server), and how fast that would be in comparison to boost::interprocess in C++? All the examples I ve seen so far only illustrate communication between same-program routines.

A simple Go example (with separate client and sever code) would be much appreciated!

问题回答

One of the first things I thought of when I read this was Stackless Python. The channels in Go remind me a lot of Stackless Python, but that s likely because (a) I ve used it and (b) the language/thoughts that they actually came from I ve never touched.

I ve never attempted to use channels as IPC, but that s probably because the alternative is likely much safer. Here s some psuedocode:

program1

chan = channel()
ipc = IPCManager(chan, None)
send_to_other_app(ipc.underlying_method)

chan.send("Ahoy!")

program2

chan = channel()
recv_from_other_app(underlying_method)
ipc = IPCManager(chan, underlying_method)

ahoy = chan.recv()

If you use a traditional IPC method, you can have channels at each side that wrap their communication on top of it. This leads to some issues in implementation, which I can t even think about how to tackle, and likely a few unexpected race conditions.

However, I agree; the ability to communicate via processes using the same flexibility of Go channels would be phenomenal (but I fear unstable).

Wrapping a simple socket with channels on each side gets you almost all of the benefits, however.

Rob has said that they are thinking a lot about how to make channels work as a (network) transparent RPC, this doesn t work at the moment, but obviously this is something they want to take the time to get it right.

In the meantime you can use the gob package which, while not a perfect and seamless solution, works quite well already.

I ve looked at doing a similar thing for wrapping the MPI library. My current thinking is to use something like

func SendHandler(comm Comm){
    // Look for sends to a process
    for {
        i := <-comm.To;
        comm.Send(i,dest);  
    }
}
func ReceiveHandler(comm Comm){
    // Look for recieves from a process
    // Ping the handler to read out
    for {
        _ = <-comm.From;
        i := comm.Recv(source);
        comm.From <- i;
     }
}

where comm.Send and comm.Recv wrap a c communications library. I m not sure how you do the setting up of a channel for two different programs though, I ve no experience in that sort of thing.





相关问题
posix_ipc python package equivalent for Windows?

Inter process communication primitives (Semaphores, Shared Memory) in python on windows? posix_ipc works great on linux, anything similar for windows?

passing events from erlang to Clojure

I m looking for a way to pass events back and forth between Clojure and erlang. has someone done this before? how should I encode the (immutable) messages in a flaxable general way? Should IPC be ...

Sockets vs named pipes for local IPC on Windows?

Are there any reasons for favoring named pipes over sockets for local IPC (both using win-api), effectiveness-wize, resource-wize or otherwise, since both behave very much alike (and likely to be ...

transparent process creation for cocoa components

I have an application A which may or may not need to spawn an application B and will communicate with it using remote messaging (via NSConnections etc.). While i know how to do this if B is started ...

Linux Pipes as Input and Output

I would like to do the following inside a C program on a Linux os: Create a PIPE using a syscall (or 2) Execute a new process using exec() Connect the process s STDIN to to the previously created ...

Shared memory vs. Go channel communication

One of Go s slogans is Do not communicate by sharing memory; instead, share memory by communicating. I am wondering whether Go allows two different Go-compiled binaries running on the same machine to ...

IPC between Python and C#

I want to pass data between a Python and a C# application in Windows (I want the channel to be bi-directional) In fact I wanna pass a struct containing data about a network packet that I ve captured ...

热门标签