options.stdio


options.stdio 选项用于配置在父进程和子进程之间建立的管道。默认情况下,子进程的 stdin、stdout 和 stderr 会重定向到 ChildProcess 对象上的相应 subprocess.stdinsubprocess.stdoutsubprocess.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:

  1. 'pipe':在子进程和父进程之间创建一个管道。管道的父端作为 child_process 对象的一个属性以 subprocess.stdio[fd] 的形式暴露给父进程。为文件描述符 0、1 和 2 创建的管道也分别作为 subprocess.stdinsubprocess.stdoutsubprocess.stderr 可用。这些不是实际的 Unix 管道,因此子进程不能通过它们的描述符文件使用它们,例如 /dev/fd/2/dev/stdout

  2. 'overlapped':与 'pipe' 相同,只是句柄上设置了 FILE_FLAG_OVERLAPPED 标志。这对于在子进程的标准输入输出句柄上进行重叠 I/O 是必要的。更多详情请参阅 文档。在非 Windows 系统上,这与 'pipe' 完全相同。

  3. 'ipc':创建一个 IPC 通道,用于在父进程和子进程之间传递消息/文件描述符。ChildProcess 最多只能有一个 IPC 标准输入输出文件描述符。设置此选项将启用 subprocess.send() 方法。如果子进程是 Node.js 进程,存在 IPC 通道将启用 process.send()process.disconnect() 方法,以及子进程内的 'disconnect''message' 事件。

    以除 process.send() 之外的任何方式访问 IPC 通道 fd,或将 IPC 通道与非 Node.js 实例的子进程一起使用,是不受支持的。

  4. 'ignore':指示 Node.js 在子进程中忽略 fd。虽然 Node.js 总是会为它生成的进程打开 fds 0、1 和 2,但将 fd 设置为 'ignore' 会导致 Node.js 打开 /dev/null 并将其附加到子进程的 fd。

  5. 'inherit':通过相应的 stdio 流传递到/来自父进程。在前三个位置中,这分别相当于 process.stdinprocess.stdoutprocess.stderr。在其他位置,则相当于 'ignore'

  6. <Stream> 对象:共享一个可读或可写的流,该流指向 tty、文件、套接字或管道,并与子进程关联。该流的底层文件描述符会在子进程中复制到对应 stdio 数组索引的 fd。该流必须有一个底层描述符(文件流在发生 'open' 事件之前没有)。

  7. 正整数:整数值被解释为父进程中打开的文件描述符。它会与子进程共享,类似于 <Stream> 对象的共享方式。在 Windows 上不支持传递套接字。

  8. nullundefined:使用默认值。对于标准输入输出文件描述符 0、1 和 2(也就是 stdin、stdout 和 stderr),会创建一个管道。对于文件描述符 3 及以上,默认值是 '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 issue 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().