options.stdio
options.stdio
选项用于配置在父进程和子进程之间建立的管道。
默认情况下,子进程的标准输入、标准输出和标准错误被重定向到 ChildProcess
对象上相应的 subprocess.stdin
、subprocess.stdout
和 subprocess.stderr
流。
这相当于将 options.stdio
设置为等于 ['pipe', 'pipe', 'pipe']
。
为方便起见,options.stdio
可能是以下字符串之一:
'pipe'
: 相当于['pipe', 'pipe', 'pipe']
(默认)'overlapped'
: 相当于['overlapped', 'overlapped', 'overlapped']
'ignore'
: 相当于['ignore', 'ignore', 'ignore']
'inherit'
: 相当于['inherit', 'inherit', 'inherit']
或[0, 1, 2]
否则,options.stdio
的值是一个数组,其中每个索引对应于子进程中的文件描述符。
文件描述符 0、1 和 2 分别对应于标准输入、标准输出和标准错误。
可以指定额外的文件描述符以在父进程和子进程之间创建额外的管道。
该值是以下之一:
-
'pipe'
:在子进程和父进程之间创建管道。 管道的父端作为subprocess.stdio[fd]
的child_process
对象的属性暴露给父进程。 为文件描述符 0、1 和 2 创建的管道也可分别用作subprocess.stdin
、subprocess.stdout
和subprocess.stderr
。 目前,这些不是实际的 Unix 管道,因此子进程不能通过它们的描述符文件使用它们,例如/dev/fd/2
或/dev/stdout
。 -
'overlapped'
:与'pipe'
相同,只是在句柄上设置了FILE_FLAG_OVERLAPPED
标志。 这对于子进程的标准输入输出句柄上的重叠 I/O 是必需的。 有关更多详细信息,请参阅文档。 这与非 Windows 系统上的'pipe'
完全相同。 -
'ipc'
:创建 IPC 通道,用于在父子进程之间传递消息/文件描述符。 一个ChildProcess
最多可以有一个 IPC 标准输入输出文件描述符。 设置此选项将启用subprocess.send()
方法。 如果子进程是 Node.js 进程,则 IPC 通道的存在将启用process.send()
和process.disconnect()
方法,以及子进程中的'disconnect'
和'message'
事件。不支持以
process.send()
以外的任何方式访问 IPC 通道文件描述符、或者将 IPC 通道与非 Node.js 实例的子进程一起使用。 -
'ignore'
:指示 Node.js 忽略子进程中的文件描述符。 虽然 Node.js 将始终为其衍生的进程打开文件描述符 0、1 和 2,但将文件描述符设置为'ignore'
将导致 Node.js 打开/dev/null
并将其附加到子进程的文件描述符。 -
'inherit'
:通过相应的标准输入输出流传入/传出父进程。 在前三个位置,这分别相当于process.stdin
、process.stdout
和process.stderr
。 在任何其他位置,相当于'ignore'
。 -
<Stream> 对象:与子进程共享引用终端、文件、套接字或管道的可读或可写流。 流的底层文件描述符在子进程中复制到对应于
stdio
数组中的索引的文件描述符。 流必须有底层描述符(文件流在'open'
事件发生之前没有)。 -
正整数:该整数值被解释为当前在父进程中打开的文件描述符。 它与子进程共享,类似于共享 <Stream> 对象的方式。 Windows 不支持传入套接字。
-
null
、undefined
:使用默认值。 对于标准输入输出文件描述符 0、1 和 2(换句话说,标准输入、标准输出和标准错误),创建管道。 对于文件描述符 3 及以上,默认值为'ignore'
。
const { spawn } = require('node:child_process');
// 子进程将使用父进程的标准输入输出。
spawn('prg', [], { stdio: 'inherit' });
// 衍生仅共享标准错误的子进程。
spawn('prg', [], { stdio: ['pipe', 'pipe', process.stderr] });
// 打开额外的文件描述符=4,以与呈现 startd 风格界面的程序进行交互。
spawn('prg', [], { stdio: ['pipe', null, null, null, 'pipe'] });
值得注意的是,当父子进程之间建立了 IPC 通道,并且子进程是 Node.js 进程时,则子进程会在未引用 IPC 通道的情况下启动(使用 unref()
),直到子进程为 'disconnect'
事件或 'message'
事件注册事件句柄。
这允许子进程正常退出,而进程不会被打开的 IPC 通道保持打开状态。
在类 Unix 操作系统上,child_process.spawn()
方法在将事件循环与子进程分离之前同步执行内存操作。
具有大量内存占用的应用程序可能会发现频繁的 child_process.spawn()
调用是一个瓶颈。
有关更多信息,请参阅 V8 问题 7381。
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']
.
For convenience, options.stdio
may be one of the following strings:
'pipe'
: equivalent to['pipe', 'pipe', 'pipe']
(the default)'overlapped'
: equivalent to['overlapped', 'overlapped', 'overlapped']
'ignore'
: equivalent to['ignore', 'ignore', 'ignore']
'inherit'
: equivalent to['inherit', 'inherit', 'inherit']
or[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'
: 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. Currently, 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'
: 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'
: 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.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'
: 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'
: 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> 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). -
Positive integer: The integer value is interpreted as a file descriptor that is currently 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
: 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'] });
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.
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.
See also: child_process.exec()
and child_process.fork()
.