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

  • modulePath <string> 要在子进程中运行的模块。
  • args <string[]> 字符串参数的列表。
  • options <Object>

    • cwd <string> 子进程的当前工作目录。
    • detached <boolean> 准备子进程独立于其父进程运行。具体行为取决于平台,参阅 options.detached
    • env <Object> 环境变量的键值对。默认值: process.env
    • execPath <string> 用于创建子进程的可执行文件。
    • execArgv <string[]> 传给可执行文件的字符串参数的列表。默认值: process.execArgv
    • silent <boolean> 如果为 true,则子进程的 stdin、stdout 和 stderr 将会被输送到父进程,否则它们将会继承自父进程,详见 child_process.spawn()stdio 中的 'pipe''inherit' 选项。默认值: false
    • stdio <Array> | <string> 参阅 child_process.spawn()stdio。当提供此选项时,则它覆盖 silent 选项。如果使用了数组变量,则它必须包含一个值为 'ipc' 的元素,否则将会抛出错误。例如 [0, 1, 2, 'ipc']
    • windowsVerbatimArguments <boolean> 在 Windows 上不为参数加上引号或转义。在 Unix 上则忽略。默认值: false
    • uid <number> 设置进程的用户标识,参阅 setuid(2)
    • gid <number> 设置进程的群组标识,参阅 setgid(2)
  • 返回: <ChildProcess>

child_process.fork() 方法是 child_process.spawn() 的一个特例,专门用于衍生新的 Node.js 进程。 与 child_process.spawn() 一样返回 ChildProcess 对象。 返回的 ChildProcess 将会内置一个额外的通信通道,允许消息在父进程和子进程之间来回传递。 详见 subprocess.send()

记住,衍生的 Node.js 子进程独立于父进程,但两者之间建立的 IPC 通信通道除外。 每个进程都有自己的内存,带有自己的 V8 实例。 由于需要额外的资源分配,因此不建议衍生大量的 Node.js 子进程。

默认情况下, child_process.fork() 将会使用父进程的 process.execPath 来衍生新的 Node.js 实例。 options 对象中的 execPath 属性允许使用其他的执行路径。

使用自定义的 execPath 启动的 Node.js 进程将会使用文件描述符(在子进程上使用环境变量 NODE_CHANNEL_FD 标识)与父进程通信。

fork(2) 的 POSIX 系统调用不同, child_process.fork() 不会克隆当前的进程。

child_process.spawn() 中可用的 shell 选项在 child_process.fork() 中不支持,如果设置则将会被忽略。

  • modulePath <string> The module to run in the child.
  • args <string[]> List of string arguments.
  • options <Object>

    • cwd <string> Current working directory of the child process.
    • detached <boolean> Prepare child to run independently of its parent process. Specific behavior depends on the platform, see options.detached).
    • env <Object> Environment key-value pairs. Default: process.env.
    • execPath <string> Executable used to create the child process.
    • execArgv <string[]> List of string arguments passed to the executable. Default: process.execArgv.
    • silent <boolean> If true, stdin, stdout, and stderr of the child will be piped to the parent, otherwise they will be inherited from the parent, see the 'pipe' and 'inherit' options for child_process.spawn()'s stdio for more details. Default: false.
    • stdio <Array> | <string> See child_process.spawn()'s stdio. When this option is provided, it overrides silent. If the array variant is used, it must contain exactly one item with value 'ipc' or an error will be thrown. For instance [0, 1, 2, 'ipc'].
    • windowsVerbatimArguments <boolean> No quoting or escaping of arguments is done on Windows. Ignored on Unix. Default: false.
    • uid <number> Sets the user identity of the process (see setuid(2)).
    • gid <number> Sets the group identity of the process (see setgid(2)).
  • Returns: <ChildProcess>

The child_process.fork() method is a special case of child_process.spawn() used specifically to spawn new Node.js processes. Like child_process.spawn(), a ChildProcess object is returned. The returned ChildProcess will have an additional communication channel built-in that allows messages to be passed back and forth between the parent and child. See subprocess.send() for details.

Keep in mind that spawned Node.js child processes are independent of the parent with exception of the IPC communication channel that is established between the two. Each process has its own memory, with their own V8 instances. Because of the additional resource allocations required, spawning a large number of child Node.js processes is not recommended.

By default, child_process.fork() will spawn new Node.js instances using the process.execPath of the parent process. The execPath property in the options object allows for an alternative execution path to be used.

Node.js processes launched with a custom execPath will communicate with the parent process using the file descriptor (fd) identified using the environment variable NODE_CHANNEL_FD on the child process.

Unlike the fork(2) POSIX system call, child_process.fork() does not clone the current process.

The shell option available in child_process.spawn() is not supported by child_process.fork() and will be ignored if set.