child_process(子进程)


稳定性: 2 - 稳定

child_process 模块提供了衍生子进程的功能,与 popen(3) 类似但不完全相同。 主要由 child_process.spawn() 提供:

const { spawn } = require('child_process');
const ls = spawn('ls', ['-lh', '/usr']);

ls.stdout.on('data', (data) => {
  console.log(`输出:${data}`);
});

ls.stderr.on('data', (data) => {
  console.log(`错误:${data}`);
});

ls.on('close', (code) => {
  console.log(`子进程退出码:${code}`);
});

默认情况下,Node.js 的父进程与衍生的子进程之间会建立 stdinstdoutstderr 的管道。

child_process.spawn() 用于异步地衍生子进程,且不会阻塞 Node.js 事件循环。 child_process.spawnSync() 则同步地衍生子进程,但会阻塞事件循环直到衍生的子进程退出或被终止。

child_process 模块还提供了其他一些同步和异步的方法。 每个方法都是基于 child_process.spawn()child_process.spawnSync() 实现的。

  • child_process.exec(): 衍生一个 shell 并在 shell 上运行命令,当完成时传入 stdoutstderr 到回调函数。
  • child_process.execFile(): 类似 child_process.exec(),但直接衍生命令且无需先衍生 shell。
  • child_process.fork(): 衍生一个新的 Node.js 进程,并通过 IPC 通讯通道来调用指定的模块,该通道允许父进程与子进程之间相互发送信息。
  • child_process.execSync(): child_process.exec() 的同步版本,会阻塞 Node.js 事件循环。
  • child_process.execFileSync(): child_process.execFile() 的同步版本,会阻塞 Node.js 事件循环。

对于某些用例,比如自动化的 shell 脚本,同步的方法更方便。 但大多数情况下,同步的方法会明显影响性能,因为它会拖延事件循环直到衍生进程完成。

Stability: 2 - Stable

The child_process module provides the ability to spawn child processes in a manner that is similar, but not identical, to popen(3). This capability is primarily provided by the child_process.spawn() function:

const { spawn } = require('child_process');
const ls = spawn('ls', ['-lh', '/usr']);

ls.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

ls.stderr.on('data', (data) => {
  console.log(`stderr: ${data}`);
});

ls.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});

By default, pipes for stdin, stdout, and stderr are established between the parent Node.js process and the spawned child. These pipes have limited (and platform-specific) capacity. If the child process writes to stdout in excess of that limit without the output being captured, the child process will block waiting for the pipe buffer to accept more data. This is identical to the behavior of pipes in the shell. Use the { stdio: 'ignore' } option if the output will not be consumed.

The child_process.spawn() method spawns the child process asynchronously, without blocking the Node.js event loop. The child_process.spawnSync() function provides equivalent functionality in a synchronous manner that blocks the event loop until the spawned process either exits or is terminated.

For convenience, the child_process module provides a handful of synchronous and asynchronous alternatives to child_process.spawn() and child_process.spawnSync(). Note that each of these alternatives are implemented on top of child_process.spawn() or child_process.spawnSync().

For certain use cases, such as automating shell scripts, the synchronous counterparts may be more convenient. In many cases, however, the synchronous methods can have significant impact on performance due to stalling the event loop while spawned processes complete.