从两个进程访问套接字
Erlang 中是否允许一个进程读取套接字,另一个进程写入套接字? 我已经尝试过了,它似乎有效,但我想知道它是否万无一失。
Is it allowed to have one process reading from and another process writing to a socket in Erlang?
I have tried it and it appears to work but I would like to know if it's foolproof.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我从源代码中理解,(至少是 gen_tcp)套接字发送/接收归结为用于发送的 erlang:port_command 和用于套接字端口上的接收的 erlang:port_control (请参阅 prim_inet.erl)。
对于port_command:“如果端口繁忙,则调用进程将暂停,直到端口不再繁忙。” port_control也是同步操作。
如果我错了,请纠正我,但使用多个进程读取和写入套接字似乎是完全安全的。
As I understand it from the source code, a (gen_tcp, at least) socket send/recv boils down to an erlang:port_command for the send and an erlang:port_control for the recv on the socket port (see prim_inet.erl).
For port_command: "if the port is busy, the calling process will be suspended until the port is not busy anymore." The port_control is also a synchronous operation.
Correct me if I'm wrong, but it would appear to be completely safe to use multiple processes to read and write to a socket.
我很确定我记得这样做没有任何问题。
I am pretty sure I remember doing this without any problems.
他们中的任何人都可以编写,但如果您的所有进程都可以接收,那就没有意义了。它会使所有没有预料到消息的人的邮箱溢出。你只需要定义一个Pid来接收数据包,默认情况下它是创建套接字的任何Pid。你可以通过设置控制进程来设置任何Pid来控制套接字。
Anyone of them can write, but it wouldn't make sense if all your processes could receive. It would overflow the mailboxes of all the ones not anticipating messages. You need to define only one Pid to receive packets, by default it's whatever Pid the socket was created in. You can set any Pid to control the socket by setting the controlling process.