subprocess.send(message[, sendHandle[, options]][, callback])
message<Object>sendHandle<Handle> | <undefined>undefined,或者是net.Socket、net.Server或dgram.Socket对象。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 processes
( 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 { fork } = require('node:child_process');
const forkedProcess = fork(`${__dirname}/sub.js`);
forkedProcess.on('message', (message) => {
console.log('PARENT got message:', message);
});
// Causes the child to print: CHILD got message: { hello: 'world' }
forkedProcess.send({ hello: 'world' });import { fork } from 'node:child_process';
const forkedProcess = fork(`${import.meta.dirname}/sub.js`);
forkedProcess.on('message', (message) => {
console.log('PARENT got message:', message);
});
// Causes the child to print: CHILD got message: { hello: 'world' }
forkedProcess.send({ hello: 'world' });然后子脚本 'sub.js' 可能看起来像这样:
【And then the child script, 'sub.js' might look like this:】
process.on('message', (message) => {
console.log('CHILD got message:', message);
});
// 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 process to send messages back to the parent process.】
发送 {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' 事件上注册时的第二个参数接收该对象。接收到并缓存在套接字中的任何数据将不会发送给子进程。在 Windows 上不支持发送 IPC 套接字。
【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 process 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. Sending IPC sockets is
not supported on Windows.】
可选的 callback 是一个函数,它在消息发送后但子进程可能尚未接收消息之前被调用。该函数接受一个参数:成功时为 null,失败时为一个 Error 对象。
【The optional callback is a function that is invoked after the message is
sent but before the child process 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 函数来实现流量控制。