返回

child_process — child process (子进程) - Node.js

前端

什么是子进程?

子进程是父进程派生出来的另一个进程。子进程是一个独立的实体,可以执行自己的任务,而不需要父进程的直接控制。子进程通常是并发进行的,共享父进程的资源,例如内存、文件和套接字。在 Node.js 中,使用 child_process 模块来创建和管理子进程。

child_process 模块概述

child_process 模块提供了一组方法来创建和控制子进程。这些方法包括:

  • exec():执行一个命令,并将输出作为字符串返回。
  • execFile():执行一个可执行文件,并将输出作为字符串返回。
  • fork():创建一个子进程,该子进程是父进程的副本。
  • spawn():创建一个子进程,该子进程可以与父进程进行通信。

方法的深入解析

exec()方法

exec()方法允许你在当前的进程中执行一个命令。语法格式如下:

exec(command, [options], callback)

其中:

  • command:要执行的命令。
  • options:用于配置子进程的参数。
  • callback:在子进程执行完成后调用的函数。
const childProcess = require('child_process');

childProcess.exec('ls -l', (error, stdout, stderr) => {
  if (error) {
    console.error(`exec error: ${error}`);
    return;
  }

  console.log(`stdout: ${stdout}`);
  console.log(`stderr: ${stderr}`);
});

execFile() 方法

execFile() 方法允许你在当前的进程中执行一个可执行文件。语法格式如下:

execFile(file, [args], [options], callback)

其中:

  • file:要执行的可执行文件路径。
  • args:传递给可执行文件作为参数的数组。
  • options:用于配置子进程的参数。
  • callback:在子进程执行完成后调用的函数。
const childProcess = require('child_process');

childProcess.execFile('node', ['--version'], (error, stdout, stderr) => {
  if (error) {
    console.error(`execFile error: ${error}`);
    return;
  }

  console.log(`stdout: ${stdout}`);
  console.log(`stderr: ${stderr}`);
});

fork() 方法

fork() 方法允许你在当前的进程中创建一个子进程,该子进程是父进程的副本。语法格式如下:

fork([modulePath], [args], [options])

其中:

  • modulePath:要加载到子进程的模块路径。
  • args:传递给模块作为参数的数组。
  • options:用于配置子进程的参数。
const childProcess = require('child_process');

const child = childProcess.fork('child.js', ['arg1', 'arg2']);

child.on('message', (message) => {
  console.log(`message from child: ${message}`);
});

child.send('Hello from parent');

spawn() 方法

spawn() 方法允许你在当前的进程中创建一个子进程,该子进程可以与父进程进行通信。语法格式如下:

spawn(command, [args], [options])

其中:

  • command:要执行的命令。
  • args:传递给命令作为参数的数组。
  • options:用于配置子进程的参数。
const childProcess = require('child_process');

const child = childProcess.spawn('ls', ['-l']);

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

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

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

子进程与线程的区别

子进程与线程是不同的。子进程是一个独立的进程,具有自己的地址空间和资源。线程是同一个进程的不同执行流,共享同一个地址空间和资源。这意味着子进程比线程更独立,但创建和管理子进程也比线程更昂贵。

何时使用 child_process 模块?

在以下情况下,可以使用 child_process 模块:

  • 当你需要执行一个命令并获取其输出时。
  • 当你需要执行一个可执行文件时。
  • 当你需要创建一个子进程来执行一个任务时。
  • 当你需要创建一个子进程来与父进程进行通信时。

提示与建议

  • 当使用 child_process 模块时,请记住以下几点:
  • 使用 child_process.exec() 方法时,最好将 stdout 和 stderr 参数传递给回调函数,以便可以捕获子进程的输出和错误。
  • 使用 child_process.execFile() 方法时,最好将 stdout 和 stderr 参数传递给回调函数,以便可以捕获子进程的输出和错误。
  • 使用 child_process.fork() 方法时,最好使用 child.on('message') 事件侦听器来监听子进程发出的消息。
  • 使用 child_process.spawn() 方法时,最好使用 child.stdout.on('data')child.stderr.on('data') 事件侦听器来监听子进程的输出和错误。

总结

child_process 模块是 Node.js 中用于创建和控制子进程的强大工具。它提供了多种方法,可以帮助你轻松地执行命令、执行可执行文件、创建子进程并与子进程进行通信。

补充

exec() 方法与 execFile() 方法的比较

特点 exec() 方法 execFile() 方法
语法 exec(command, [options], callback) execFile(file, [args], [options], callback)
执行对象 命令 可执行文件
参数 command, [options], callback file, [args], [options], callback
返回值 stdout, stderr stdout, stderr

fork() 方法与 spawn() 方法的比较

特点 fork() 方法 spawn() 方法
语法 fork([modulePath], [args], [options]) spawn(command, [args], [options])
执行对象 模块 命令
参数 [modulePath], [args], [options] command, [args], [options]
返回值 子进程对象 子进程对象

其他资源