在同一主机上的Go程序之间,TCP是最好的通信方法吗?

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

Is TCP the best method for communication between Go programs in the same host?

问题

假设我在本地主机上有两个独立运行的 Go 程序,从性能角度来看,TCP 是在这两个程序之间传输数据的最佳方法吗?

英文:

Suppose I have two separated Go programs running in my localhost, is TCP the best method for transferring data between the two programs in terms of performance?

答案1

得分: 1

简短的回答是不。TCP/IP协议栈很慢,特别是TCP部分。所以从性能角度来看,最好使用本地进程间通信方法,比如应用程序之间的共享内存或Unix套接字。

如果你必须使用网络协议栈进行通信(比如计划在主机之间迁移应用程序),那么UDP或原始套接字是性能最好的选择。

只有在以下情况下:

  1. 必须使用网络,并且
  2. 需要可靠的通信通道,那么TCP是一个不错的选择。

所以,请仔细考虑你的需求,并决定是否是最适合你的方法。

英文:

The short answer is no. The TCP/IP stack is slow, especially the TCP part. So in terms of performance you better use local inter-process communication methods, like a shared memory between your applications or Unix sockets.

If you MUST use a network stack to communicate (say, you plan to move applications between hosts), then UDP or raw sockets are the best options in terms of performance.

And only if you:

  1. must use a network and
  2. you need a reliable communication channel, then TCP is a good option.

So just walk through your requirements and decide if it is a best method for you.

答案2

得分: 0

这样做可以使两个程序在不同的计算机上运行,并提供额外的自由。但从性能角度来看,这并不是最佳选择。

为了获得良好的性能,可以考虑使用共享内存。
https://en.wikipedia.org/wiki/Shared_memory
也许你可以更详细地描述一下你想要做什么。

英文:

It would work and give additional freedom to have the two programs run on different computers. But it is not the best in terms of performance.

For good performance, shared memory comes to mind.
https://en.wikipedia.org/wiki/Shared_memory
Maybe you could describe a bit more what exactly you want to do.

huangapple
  • 本文由 发表于 2017年5月30日 00:14:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/44246728.html
匿名

发表评论

匿名网友

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

确定