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


版本历史
版本变更
v4.0.0

现在支持 callback 参数。

v0.7.0

新增于: v0.7.0

  • message <Object>

  • sendHandle <Handle>

  • options <Object> options 参数(如果存在)是用于参数化某些类型句柄的发送的对象。options 支持以下属性:

    \options <Object> The options argument, if present, is an object used to parameterize the sending of certain types of handles. options supports the following properties:

    • keepOpen <boolean> 当传入 net.Socket 实例时可以使用的值。当为 true 时,套接字在发送过程中保持打开状态。默认值:false

      \keepOpen <boolean> A value that can be used when passing instances of net.Socket. When true, the socket is kept open in the sending process. Default: false.

  • callback <Function>

  • 返回:<boolean>

    \Returns: <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);
 });
} 

AltStyle によって変換されたページ (->オリジナル) /