如何在Go中实现进程间通讯? [英] How to implement inter-process communication in Go?

查看:1901
本文介绍了如何在Go中实现进程间通讯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Go编写负载平衡的服务器系统.

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.

我已经找到了网络,但是现在我需要找到一种最佳的方式来使负载均衡器与本地应用程序服务器进行通信.使用localhost-网络似乎并不是最佳选择.

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.

我正在尝试通过shmgetshmat系统调用共享内存,但是没有找到任何可行的示例,并且syscall程序包也完全没有文档.

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.

有人可以为我提供如何使用这些呼叫的示例,还是可以在Go上进行IPC的可行替代方案?

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

推荐答案

Go具有内置的RPC系统( http://golang.org/pkg/rpc/),以简化Go进程之间的通信.

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

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

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

如果不进行基准测试,则不应关闭本地网络.例如,Chrome使用命名管道进行IPC,并在进程之间传输大量数据(例如渲染的位图):

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:

我们主要的进程间通信原语是命名管道.在 Linux& OS X,我们使用一个socketpair()

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

- http://www.chromium.org/developers/设计文档/进程间通信

如果命名管道足以满足要求,那么它们可能足以满足您的用例.另外,如果编写得不错,则可以开始使用命名管道(因为很简单),然后如果发现命名管道的性能不够好(无论使用哪种语言,共享内存都不容易),然后切换到共享内存.

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).

这篇关于如何在Go中实现进程间通讯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆