options.stdio
options.stdio 选项用于配置在父进程和子进程之间建立的管道。默认情况下,子进程的 stdin、stdout 和 stderr 会重定向到 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'](默认值)'overlapped':等同于['overlapped', 'overlapped', 'overlapped']'ignore':等同于['ignore', 'ignore', 'ignore']'inherit':相当于['inherit', 'inherit', 'inherit']或[0, 1, 2]
否则,options.stdio 的值是一个数组,其中每个索引对应子进程中的一个文件描述符(fd)。fd 0、1 和 2 分别对应标准输入(stdin)、标准输出(stdout)和标准错误(stderr)。可以指定额外的文件描述符以在父进程和子进程之间创建额外的管道。其值可以是以下之一:
🌐 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':在子进程和父进程之间创建管道。管道的父端通过child_process对象上的subprocess.stdio[fd]属性暴露给父进程。为文件描述符 0、1 和 2 创建的管道也分别可作为subprocess.stdin、subprocess.stdout和subprocess.stderr使用。这些并不是真正的 Unix 管道,因此子进程不能通过它们的描述符文件使用它们,例如/dev/fd/2或/dev/stdout。 -
'overlapped':与'pipe'相同,不同之处在于句柄上设置了FILE_FLAG_OVERLAPPED标志。这对于子进程的 stdio 句柄进行重叠 I/O 是必要的。有关更多详细信息,请参见 文档。在非 Windows 系统上,这与'pipe'完全相同。 -
'ipc':创建一个 IPC 通道,用于在父进程和子进程之间传递消息/文件描述符。一个ChildProcess最多可以有一个 IPC 标准输入输出文件描述符。设置此选项将启用subprocess.send()方法。如果子进程是 Node.js 实例,IPC 通道的存在将启用process.send()和process.disconnect()方法,以及子进程中的'disconnect'和'message'事件。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 总是会为其生成的进程打开文件描述符 0、1 和 2,但将 fd 设置为'ignore'会导致 Node.js 打开/dev/null并将其附加到子进程的 fd 上。 -
'inherit':通过相应的 stdio 流传输到/来自父进程。在前三个位置上,分别相当于process.stdin、process.stdout和process.stderr。在其他任何位置上,相当于'ignore'。 -
<Stream> 对象:与子进程共享一个可读或可写的流,该流可以是 tty、文件、套接字或管道。该流的底层文件描述符会在子进程中被复制到
stdio数组中对应索引的 fd。该流必须有底层描述符(文件流在'open'事件发生之前不会启动)。
注意: 虽然从技术上可以将 stdin 作为可写流或 stdout/stderr 作为可读流传递,但不推荐这样做。可读流和可写流有不同的行为,错误使用它们(例如,在需要可写流的地方传递可读流)可能导致意外结果或错误。此做法不被建议,因为在流发生错误时可能导致未定义行为或回调丢失。始终确保 stdin 用作可读流,stdout/stderr 用作可写流,以保持父进程与子进程之间预期的数据流。
7. 正整数:整数值被解释为父进程中打开的文件描述符。它会与子进程共享,类似于 <Stream> 对象的共享方式。在 Windows 上不支持传递套接字。
null、undefined:使用默认值。对于标准输入输出文件描述符 0、1 和 2(也就是 stdin、stdout 和 stderr),会创建一个管道。对于文件描述符 3 及以上,默认值是'ignore'。
const { spawn } = require('node:child_process');
const process = require('node: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'] });import { spawn } from 'node:child_process';
import process from 'node: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 实例时,子进程会以未引用(使用 unref())的方式启动 IPC 通道,直到子进程为 'disconnect' 事件或 'message' 事件注册事件处理程序为止。这允许子进程正常退出,而不会因为打开的 IPC 通道而导致进程保持运行。
另请参见:child_process.exec() 和 child_process.fork()。
🌐 It is worth noting that when an IPC channel is established between the
parent and child processes, and the child process is a Node.js instance,
the child process is launched with the IPC channel unreferenced (using
unref()) until the child process registers an event handler for the
'disconnect' event or the 'message' event. This allows the
child process to exit normally without the process being held open by the
open IPC channel.
See also: child_process.exec() and child_process.fork().