Node.js 中两个不同进程之间的通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10213501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Communicating between two different processes in Node.js
提问by Alexey Kamenskiy
The issue is:
问题是:
Lets assume we have two Node.js processes running:
example1.jsandexample2.js.In
example1.jsthere is functionfunc1(input)which returnsresult1as a result.Is there a way from within
example2.jsto callfunc1(input)and obtainresult1as the outcome?
假设我们有两个 Node.js 进程在运行:
example1.js和example2.js.在
example1.js那里有作为结果func1(input)返回的函数result1。有没有办法从内部
example2.js调用func1(input)并获得result1作为结果?
From what I've learned about Node.js, I have only found one solution which uses sockets for communication. This is less than ideal however because it would require one process listening on a port. If possible I wish to avoid that.
根据我对 Node.js 的了解,我只找到了一种使用套接字进行通信的解决方案。然而,这并不理想,因为它需要一个进程侦听端口。如果可能,我希望避免这种情况。
EDIT:After some questions I'd love to add that in hierarchy example1.jscannot be child process of example2.js, but rather the opposite. Also if it helps -- there can be only one example1.jsprocessing its own data and many example2.js's processing own data + data from first process.
编辑:在一些问题之后,我想补充一点,在层次结构example1.js中不能是 的子进程example2.js,而是相反。此外,如果有帮助 - 只能有一个example1.js处理自己的数据,而许多example2.js处理自己的数据+来自第一个进程的数据。
回答by Linus Gustav Larsson Thiel
The use case you describe makes me think of dnode, with which you can easily expose functions to be called by different processes, coordinated by dnode, which uses network sockets (and socket.io, so you can use the same mechanism in the browser).
您描述的用例让我想到了dnode,您可以使用它轻松公开要由不同进程调用的函数,由 dnode 协调,它使用网络套接字(和 socket.io,因此您可以在浏览器中使用相同的机制) .
Another approach would be to use a message queue, there are many good bindings for different message queues.
The simplest way to my knowledge, is to use child_process.fork():
据我所知,最简单的方法是使用child_process.fork():
This is a special case of the
spawn()functionality for spawning Node processes. In addition to having all the methods in a normal ChildProcess instance, the returned object has a communication channel built-in. The channel is written to withchild.send(message, [sendHandle])and messages are received by a'message'event on the child.
这是生成
spawn()Node 进程功能的一个特例。除了在普通 ChildProcess 实例中包含所有方法之外,返回的对象还具有内置的通信通道。通道写入 withchild.send(message, [sendHandle])并且消息由'message'child 上的事件接收。
So, for your example, you could have example2.js:
因此,对于您的示例,您可以使用 example2.js:
var fork = require('child_process').fork;
var example1 = fork(__dirname + '/example1.js');
example1.on('message', function(response) {
console.log(response);
});
example1.send({func: 'input'});
And example1.js:
和example1.js:
function func(input) {
process.send('Hello ' + input);
}
process.on('message', function(m) {
func(m);
});
回答by Kaicui
May be you should try Messenger.js. It can do IPC in a handy way.
也许你应该试试Messenger.js。它可以方便地进行 IPC。
You don't have to do the communication between the two processes by yourself.
您不必自己进行两个进程之间的通信。
回答by Alexander Mills
Use Redis as a message bus/broker.
使用 Redis 作为消息总线/代理。
https://redis.io/topics/pubsub
https://redis.io/topics/pubsub
You can also use socket messaging like ZeroMQ, which are point to point / peer to peer, instead of using a message broker like Redis.
您还可以使用像 ZeroMQ 这样的套接字消息传递,它们是点对点/点对点的,而不是使用像 Redis 这样的消息代理。
How does this work?
这是如何运作的?
With Redis, in both your node applications you have two Redis clients doing pub/sub. So each node.js app would have a publisher and subscriber client (yes you need 2 clients per node process for Redis pub/sub)
使用 Redis,在您的两个节点应用程序中,您都有两个 Redis 客户端执行发布/订阅。因此,每个 node.js 应用程序都会有一个发布者和订阅者客户端(是的,Redis 发布/订阅的每个节点进程需要 2 个客户端)
With ZeroMQ, you can send messages via IPC channels, directly between node.js processes, (no broker involved - except perhaps the OS itself..).
使用 ZeroMQ,您可以通过 IPC 通道直接在 node.js 进程之间发送消息(不涉及代理 - 除了操作系统本身......)。

