如何在Go中实现进程间通信?

huangapple go评论84阅读模式
英文:

How to implement inter-process communication in Go?

问题

我正在使用Go编写一个负载均衡服务器系统。

负载均衡服务器将与多个应用服务器通信并处理请求。这些服务器可以在同一台机器上运行,也可以在网络上运行。

我已经解决了网络问题,但现在我需要找到一种最佳方式,让负载均衡器与本地应用服务器通信。使用localhost网络似乎远非最佳选择。

我尝试通过shmgetshmat系统调用来共享内存,但没有找到任何可行的示例,而且syscall包也完全没有文档。

有人能给我提供如何使用这些调用的示例,或者在Go中进行IPC的可行替代方案吗?

英文:

I’m writing a load balanced server system in Go.

The load balancing server will communicate with several application servers and process requests. These servers can both, be running on the same machine or on the network.

I already figured the networking out but now I need to find an optimal way for the load-balancer to communicate with a local application server. Using localhost-networking seems far from optimal.

I’m trying to share memory via the shmget and shmat system-calls but haven’t found any working examples and the syscall package is also completely undocumented.

Can someone provide me with an example of how to use these calls or a realistic alternative that works on Go for doing IPC?

答案1

得分: 75

Go有一个内置的RPC系统(http://golang.org/pkg/rpc/),用于Go进程之间的简单通信。

另一个选项是通过网络连接发送gob编码的数据(http://blog.golang.org/2011/03/gobs-of-data.html)。

在没有进行基准测试之前,不应该忽视本地网络。例如,Chrome使用命名管道进行IPC,并在进程之间传输大量数据(例如渲染的位图):

我们的主要进程间通信原语是命名管道。在Linux和OS X上,我们使用socketpair()

-- http://www.chromium.org/developers/design-documents/inter-process-communication

如果命名管道对于这个用例足够好,那么它们可能对于您的用例也足够好。此外,如果您编写得很好,您可以开始使用命名管道(因为它很容易),然后在发现命名管道的性能不够好时切换到共享内存(无论使用哪种语言,共享内存都不容易)。

英文:

Go has a built-in RPC system (http://golang.org/pkg/rpc/) for easy communication between Go processes.

Another option is to send gob-encoded data (http://blog.golang.org/2011/03/gobs-of-data.html) via network connection.

You shouldn't dismiss local networking without benchmarking. For example Chrome uses named pipes for IPC and they transfer a lot of data (e.g. rendered bitmaps) between processes:

> Our main inter-process communication primitive is the named pipe. On
> Linux & OS X, we use a socketpair()

-- http://www.chromium.org/developers/design-documents/inter-process-communication

If named pipes are good enough for that, they are probably good enough for your use case. Plus, if you write things well, you could start using named pipes (because it's easy) and then switch to shared memory if you find performance of named pipes not good enough (shared memory is not easy regardless of the language).

答案2

得分: 18

我建议看一下0mq。它是一个消息传递库,旨在快速且易于使用,无论您是在网络上使用它,还是用于本地IPC,甚至是线程间通信。它处理了许多IPC的棘手问题,比如在接收方过载时使发送方停止发送请求、消息分帧以及失败后重新连接。它还提供了许多语言的绑定,包括Go语言,这使得它对于将使用不同语言编写的系统连接在一起非常有用。

英文:

I'd suggest looking at 0mq. It's a messaging library designed to be be fast and easy whether you use it over the network, for local IPC, or even inter-thread communication. It handles a lot of the tricky bits of IPC, like making senders back off sending requests if the receiver is getting overloaded, message framing, and reconnecting after failure. And it has bindings for LOTS of languages, including Go, which makes it useful for wiring together systems written in different languages.

答案3

得分: 7

Go的内置RPC包仍然可用,但请注意它因为“难以修复的未解决错误”而被冻结(详见https://github.com/golang/go/issues/16844)。

英文:

Go's builtin RPC package is still usable, but note that it's frozen due to outstanding bugs that are hard to fix (see https://github.com/golang/go/issues/16844 for details).

huangapple
  • 本文由 发表于 2012年2月21日 02:36:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/9366550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定