options.stdio
options.stdio
选项用于配置在父进程和子进程之间建立的管道。默认情况下,子进程的标准输入、标准输出和标准错误被重定向到 ChildProcess
对象上相应的 subprocess.stdin
、subprocess.stdout
和 subprocess.stderr
流。这相当于将 options.stdio
设置为等于 ['pipe', 'pipe', 'pipe']
。
¥The options.stdio
option is used to configure the pipes that are established
between the parent and child process. By default, the child's stdin, stdout,
and stderr are redirected to corresponding subprocess.stdin
,
subprocess.stdout
, and subprocess.stderr
streams on the
ChildProcess
object. This is equivalent to setting the options.stdio
equal to ['pipe', 'pipe', 'pipe']
.
为方便起见,options.stdio
可能是以下字符串之一:
¥For convenience, options.stdio
may be one of the following strings:
-
'pipe'
:相当于['pipe', 'pipe', 'pipe']
(默认)¥
'pipe'
: equivalent to['pipe', 'pipe', 'pipe']
(the default) -
'overlapped'
:相当于['overlapped', 'overlapped', 'overlapped']
¥
'overlapped'
: equivalent to['overlapped', 'overlapped', 'overlapped']
-
'ignore'
:相当于['ignore', 'ignore', 'ignore']
¥
'ignore'
: equivalent to['ignore', 'ignore', 'ignore']
-
'inherit'
:相当于['inherit', 'inherit', 'inherit']
或[0, 1, 2]
¥
'inherit'
: equivalent to['inherit', 'inherit', 'inherit']
or[0, 1, 2]
否则,options.stdio
的值是一个数组,其中每个索引对应于子进程中的文件描述符。文件描述符 0、1 和 2 分别对应于标准输入、标准输出和标准错误。可以指定额外的文件描述符以在父进程和子进程之间创建额外的管道。该值是以下之一:
¥Otherwise, the value of options.stdio
is an array where each index corresponds
to an fd in the child. The fds 0, 1, and 2 correspond to stdin, stdout,
and stderr, respectively. Additional fds can be specified to create additional
pipes between the parent and child. The value is one of the following:
-
'pipe'
:在子进程和父进程之间创建管道。管道的父端作为subprocess.stdio[fd]
对象上的child_process
对象的属性公开给父级。为 fds 0、1 和 2 创建的管道也可分别用作subprocess.stdin
、subprocess.stdout
和subprocess.stderr
。这些不是实际的 Unix 管道,因此子进程不能通过它们的描述符文件使用它们,例如/dev/fd/2
或/dev/stdout
。¥
'pipe'
: Create a pipe between the child process and the parent process. The parent end of the pipe is exposed to the parent as a property on thechild_process
object assubprocess.stdio[fd]
. Pipes created for fds 0, 1, and 2 are also available assubprocess.stdin
,subprocess.stdout
andsubprocess.stderr
, respectively. These are not actual Unix pipes and therefore the child process can not use them by their descriptor files, e.g./dev/fd/2
or/dev/stdout
. -
'overlapped'
:与'pipe'
相同,只是在句柄上设置了FILE_FLAG_OVERLAPPED
标志。这对于子进程的 stdio 句柄上的重叠 I/O 是必需的。有关详细信息,请参阅 文档。这与非 Windows 系统上的'pipe'
完全相同。¥
'overlapped'
: Same as'pipe'
except that theFILE_FLAG_OVERLAPPED
flag is set on the handle. This is necessary for overlapped I/O on the child process's stdio handles. See the docs for more details. This is exactly the same as'pipe'
on non-Windows systems. -
'ipc'
:创建一个 IPC 通道,用于在父子之间传递消息/文件描述符。一个ChildProcess
最多可以有一个 IPC stdio 文件描述符。设置此选项可启用subprocess.send()
方法。如果子进程是 Node.js 进程,IPC 通道的存在将启用process.send()
和process.disconnect()
方法,以及子进程中的'disconnect'
和'message'
事件。¥
'ipc'
: Create an IPC channel for passing messages/file descriptors between parent and child. AChildProcess
may have at most one IPC stdio file descriptor. Setting this option enables thesubprocess.send()
method. If the child is a Node.js process, the presence of an IPC channel will enableprocess.send()
andprocess.disconnect()
methods, as well as'disconnect'
and'message'
events within the child.不支持以
process.send()
以外的任何方式访问 IPC 通道 fd 或将 IPC 通道用于非 Node.js 实例的子进程。¥Accessing the IPC channel fd in any way other than
process.send()
or using the IPC channel with a child process that is not a Node.js instance is not supported. -
'ignore'
:指示 Node.js 忽略子项中的 fd。虽然 Node.js 将始终为其生成的进程打开 fds 0、1 和 2,但将 fd 设置为'ignore'
将导致 Node.js 打开/dev/null
并将其附加到子进程的 fd。¥
'ignore'
: Instructs Node.js to ignore the fd in the child. While Node.js will always open fds 0, 1, and 2 for the processes it spawns, setting the fd to'ignore'
will cause Node.js to open/dev/null
and attach it to the child's fd. -
'inherit'
:通过相应的 stdio 流传入/传出父进程。在前三个位置,这分别相当于process.stdin
、process.stdout
、process.stderr
。在任何其他位置,相当于'ignore'
。¥
'inherit'
: Pass through the corresponding stdio stream to/from the parent process. In the first three positions, this is equivalent toprocess.stdin
,process.stdout
, andprocess.stderr
, respectively. In any other position, equivalent to'ignore'
. -
<Stream> 对象:与子进程共享引用 tty、文件、套接字或管道的可读或可写流。流的底层文件描述符在子进程中复制到与
stdio
数组中的索引相对应的 fd。流必须具有底层描述符(文件流只有在'open'
事件发生后才具有)。¥<Stream> object: Share a readable or writable stream that refers to a tty, file, socket, or a pipe with the child process. The stream's underlying file descriptor is duplicated in the child process to the fd that corresponds to the index in the
stdio
array. The stream must have an underlying descriptor (file streams do not until the'open'
event has occurred). -
正整数:整数值被解释为在父进程中打开的文件描述符。它与子进程共享,类似于 <Stream> 对象的共享方式。Windows 不支持传递套接字。
¥Positive integer: The integer value is interpreted as a file descriptor that is open in the parent process. It is shared with the child process, similar to how <Stream> objects can be shared. Passing sockets is not supported on Windows.
-
null
,undefined
:使用默认值。对于 stdio fds 0、1 和 2(换句话说,stdin、stdout 和 stderr),创建了一个管道。对于 fd 3 及更高版本,默认值为'ignore'
。¥
null
,undefined
: Use default value. For stdio fds 0, 1, and 2 (in other words, stdin, stdout, and stderr) a pipe is created. For fd 3 and up, the default is'ignore'
.
const { spawn } = require('node:child_process');
// Child will use parent's stdios.
spawn('prg', [], { stdio: 'inherit' });
// Spawn child sharing only stderr.
spawn('prg', [], { stdio: ['pipe', 'pipe', process.stderr] });
// Open an extra fd=4, to interact with programs presenting a
// startd-style interface.
spawn('prg', [], { stdio: ['pipe', null, null, null, 'pipe'] });
值得注意的是,当父子进程之间建立了 IPC 通道,并且子进程是 Node.js 进程时,子进程将在未引用 IPC 通道(使用 unref()
)的情况下启动,直到子进程为该子进程注册事件处理程序 'disconnect'
事件或 'message'
事件。这允许子进程正常退出,而进程不会被打开的 IPC 通道保持打开状态。
¥It is worth noting that when an IPC channel is established between the
parent and child processes, and the child is a Node.js process, the child
is launched with the IPC channel unreferenced (using unref()
) until the
child registers an event handler for the 'disconnect'
event
or the 'message'
event. This allows the child to exit
normally without the process being held open by the open IPC channel.
在类 Unix 操作系统上,child_process.spawn()
方法在将事件循环与子进程解耦之前同步执行内存操作。具有大量内存占用的应用可能会发现频繁的 child_process.spawn()
调用成为瓶颈。有关详细信息,请参阅 V8 问题 7381。
¥On Unix-like operating systems, the child_process.spawn()
method
performs memory operations synchronously before decoupling the event loop
from the child. Applications with a large memory footprint may find frequent
child_process.spawn()
calls to be a bottleneck. For more information,
see V8 issue 7381.
也可以看看:child_process.exec()
和 child_process.fork()
。
¥See also: child_process.exec()
and child_process.fork()
.