求教:子进程如何向主进程发消息
发布于 11 年前 作者 yuenshui 4626 次浏览 最后一次编辑是 8 年前

使用cluster的时候,主进程可以向子进程发消息,通过worker.process.send 如果子进程向主进程发消息,该如何写呢???

2 回复

官网实例如下: 父进程:

var cp = require('child_process');

var n = cp.fork(__dirname + '/sub.js');

n.on('message', function(m) {
  console.log('PARENT got message:', m);
});

n.send({ hello: 'world' });

sub.js代码:

process.on('message', function(m) {
  console.log('CHILD got message:', m);
});

process.send({ foo: 'bar' });

另外可以看一下我写的一个日志:http://cnodejs.org/topic/5190eb1263e9f8a542ae7b54

谢了,原来我发送写对了。接收写错了。

回到顶部