subprocess.send(message[, sendHandle[, options]][, callback])
message<Object>sendHandle<Handle>options<Object>options参数(如果存在)是一个用于参数化发送某些类型句柄的对象。options支持以下属性:keepOpen<boolean> 一个在传递net.Socket实例时可以使用的值。当为true时,套接字在发送进程中保持打开状态。默认值:false。
callback<Function>- 返回:<boolean>
当在父进程和子进程之间建立了 IPC 通道(即使用 child_process.fork() 时),可以使用 subprocess.send() 方法向子进程发送消息。当子进程是 Node.js 实例时,可以通过 'message' 事件接收这些消息。
🌐 When an IPC channel has been established between the parent and child (
i.e. when using child_process.fork()), the subprocess.send() method can
be used to send messages to the child process. When the child process is a
Node.js instance, these messages can be received via the 'message' event.
消息会经过序列化和解析。最终得到的消息可能与最初发送的内容不完全相同。
🌐 The message goes through serialization and parsing. The resulting message might not be the same as what is originally sent.
例如,在父脚本中:
🌐 For example, in the parent script:
const cp = require('node:child_process');
const n = cp.fork(`${__dirname}/sub.js`);
n.on('message', (m) => {
console.log('PARENT got message:', m);
});
// Causes the child to print: CHILD got message: { hello: 'world' }
n.send({ hello: 'world' }); 然后子脚本 'sub.js' 可能看起来像这样:
🌐 And then the child script, 'sub.js' might look like this:
process.on('message', (m) => {
console.log('CHILD got message:', m);
});
// Causes the parent to print: PARENT got message: { foo: 'bar', baz: null }
process.send({ foo: 'bar', baz: NaN }); 子 Node.js 进程将拥有自己的 process.send() 方法,该方法允许子进程向父进程发送消息。
🌐 Child Node.js processes will have a process.send() method of their own
that allows the child to send messages back to the parent.
发送 {cmd: 'NODE_foo'} 消息时有一种特殊情况。cmd 属性中包含 NODE_ 前缀的消息是保留给 Node.js 核心使用的,不会在子进程的 'message' 事件中发出。相反,这类消息会使用 'internalMessage' 事件发出,并由 Node.js 内部处理。应用应避免使用此类消息或监听 'internalMessage' 事件,因为其行为可能会在未经通知的情况下发生变化。
🌐 There is a special case when sending a {cmd: 'NODE_foo'} message. Messages
containing a NODE_ prefix in the cmd property are reserved for use within
Node.js core and will not be emitted in the child's 'message'
event. Rather, such messages are emitted using the
'internalMessage' event and are consumed internally by Node.js.
Applications should avoid using such messages or listening for
'internalMessage' events as it is subject to change without notice.
可选的 sendHandle 参数可以传递给 subprocess.send(),用于将 TCP 服务器或套接字对象传递给子进程。子进程将会把该对象作为注册在 'message' 事件上的回调函数的第二个参数接收。任何在套接字中接收到并缓冲的数据都不会发送给子进程。
🌐 The optional sendHandle argument that may be passed to subprocess.send() is
for passing a TCP server or socket object to the child process. The child will
receive the object as the second argument passed to the callback function
registered on the 'message' event. Any data that is received
and buffered in the socket will not be sent to the child.
可选的 callback 是一个函数,在消息发送后但子进程可能尚未收到消息之前调用。该函数接收一个参数:成功时为 null,失败时为 Error 对象。
🌐 The optional callback is a function that is invoked after the message is
sent but before the child may have received it. The function is called with a
single argument: null on success, or an Error object on failure.
如果未提供 callback 函数且消息无法发送,ChildProcess 对象将触发 'error' 事件。例如,当子进程已经退出时,就可能发生这种情况。
🌐 If no callback function is provided and the message cannot be sent, an
'error' event will be emitted by the ChildProcess object. This can
happen, for instance, when the child process has already exited.
subprocess.send() 如果通道已关闭,或者未发送消息的积压超过了不宜继续发送的阈值,将返回 false。否则,该方法返回 true。可以使用 callback 函数来实现流量控制。