process.send is sync/async on *nix/Windows?

十年热恋 提交于 2019-12-22 05:31:42

问题


I have a Node.js process (not a server) that forks N child processes. At some point there might be more than 50 CPs. So I started thinking that if process.send (IPC) is truly blocking then this might be a big penalty experienced by each CP. Because what happens in my program is that each CP uses process.send to send a message to the single parent process so that the parent will do the logging, so that the logging will be synchronized. But if process.send blocks then at some point the parent process might become a bottleneck.

So the question is - is Node.js IPC blocking or non-blocking on *nix and Windows? If it is blocking, and if I or someone else really want asynchronous IPC, should I use a message queue or ZeroMQ or something?


回答1:


Process send has been set to asynchronous, see https://github.com/nodejs/node/commit/56d9584a0ead78874ca9d4de2e55b41c4056e502

"`ChildProcess.prototype.send()` and `process.send()` used to operate
synchronously but became asynchronous in commit libuv/libuv@393c1c5"

this is due to changes in the libuv code; it has a few drawbacks but if you're concerned about the parent process becoming the bottleneck you could instead use pipes for communication.

50 CPs will not be a problem, but 500 might, depending on your architecture and size of messages. At that point I would recommend a message queue that's a bit more fancy. ZeroMQ or plain redis should work.



来源:https://stackoverflow.com/questions/34627546/process-send-is-sync-async-on-nix-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!