worker.send(message[, sendHandle[, options]][, callback])


  • message <Object>
  • sendHandle <Handle>
  • options <Object> 如果 options 参数存在,它是一个用于参数化发送某些类型句柄的对象。options 支持以下属性:
    • keepOpen <boolean> 当传递 net.Socket 实例时可以使用的值。为 true 时,套接字在发送过程中保持打开。 默认值: false
  • callback <Function>
  • 返回: <boolean>

向工作进程或主进程发送消息,可选择使用句柄。

【Send a message to a worker or primary, optionally with a handle.】

在主要部分,这会向特定的工作人员发送一条消息。它与 ChildProcess.send() 完全相同。

【In the primary, this sends a message to a specific worker. It is identical to ChildProcess.send().】

在一个工作线程中,这会向主线程发送一条消息。它与 process.send() 相同。

【In a worker, this sends a message to the primary. It is identical to process.send().】

此示例将回显来自主进程的所有消息:

【This example will echo back all messages from the primary:】

if (cluster.isPrimary) {
  const worker = cluster.fork();
  worker.send('hi there');

} else if (cluster.isWorker) {
  process.on('message', (msg) => {
    process.send(msg);
  });
}