FIFO(命名管道)消息传送障碍
我计划使用 Unix 命名管道 (mkfifo) 进行简单的多进程消息传递。 消息只是一行文本。
你会阻止我这样做吗? 我应该期待哪些障碍?
我注意到了这些限制:
- 发送者在收到消息之前无法继续。
- 接收器被阻塞,直到有一些数据为止。 需要非阻塞 IO 当我们需要停止阅读时。 例如,另一个线程可能会要求这样做。
- 接收者可以在一次读取中获得许多消息。 这些都需要处理 在戒烟之前。
- 原子消息的最大长度限制为 4096 字节。 这是 Linux 上的 PIPE_BUF 限制(请参阅 man 7 pipeline)。
我将用 Python 实现消息传递。 但总体而言,障碍依然存在。
I plan to use Unix named pipes (mkfifo) for simple multi-process messaging.
A message would be just a single line of text.
Would you discourage me from that? What obstacles should I expect?
I have noticed these limitations:
- A sender cannot continue until the message is received.
- A receiver is blocked until there are some data. Nonblocking IO would be needed
when we need to stop the reading. For example, another thread could ask for that. - The receiver could obtain many messages in a single read. These have to be processed
before quiting. - The max length of an atomic message is limited by 4096 bytes. That is the PIPE_BUF limit on Linux (see man 7 pipe).
I will implement the messaging in Python. But the obstacles hold in general.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
至于您的限制,
我发现 perlipc 是一个很好的讨论各种选项,尽管它有 perl 特定代码。
As to your limitations,
I find perlipc to be a good discussion between the various options, though it has perl specific code.
发送方和接收方的阻塞都可以通过非阻塞 I/O 来解决。
FIFO 的进一步限制:
我会使用 UNIX 域套接字 来代替,它没有上述限制。
作为一个额外的好处,如果您想扩展它以在多台机器之间进行通信,则几乎不需要任何更改。 例如,只需查看 socket 上的 Python 文档页面并替换 < code>socket.AF_INET 与
socket.AF_UNIX
、(HOST, PORT)
与filename
,并且它可以正常工作。SOCK_STREAM
将为您提供类似流的行为; 也就是说,两个发送可以合并为一个接收,反之亦然。AF_UNIX
还支持SOCK_DGRAM
:保证数据报全部作为一个单元发送和读取,或者根本不发送和读取。 (类似地,AF_INET
+SOCK_STREAM
=TCP,AF_INET
+SOCK_DGRAM
=UDP。)The blocking, both on the sender side and the receiver side, can be worked around via non-blocking I/O.
Further limitations of FIFOs:
I would use UNIX domain sockets instead, which have none of the above limitations.
As an added benefit, if you want to scale it to communicate between multiple machines, it's barely any change at all. For example, just take the Python documentation page on socket and replace
socket.AF_INET
withsocket.AF_UNIX
,(HOST, PORT)
withfilename
, and it just works.SOCK_STREAM
will give you stream-like behavior; that is, two sends may be merged into one receive or vice versa.AF_UNIX
also supportsSOCK_DGRAM
: datagrams are guaranteed to be sent and read all as one unit or not at all. (Analogously,AF_INET
+SOCK_STREAM
=TCP,AF_INET
+SOCK_DGRAM
=UDP.)