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


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

    • cwd <string> 子进程的当前工作目录。
    • env <Object> 环境变量键值对。
    • 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 上是否为参数加引号或转义。默认为 false
    • uid <number> 进程的用户标识,参阅 setuid(2)
    • gid <number> 进程的群组标识,参阅 setgid(2)
  • 返回: <ChildProcess>

child_process.fork()child_process.spawn() 的一个特殊情况,用于专门衍生新的 Node.js 进程。 返回的 ChildProcess 有一个额外的内置通信通道,允许消息在父进程和子进程之间来回传递。 详见 subprocess.send()

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

默认情况下,child_process.fork() 会使用父进程中的 process.execPath 衍生新的 Node.js 实例。 optionsexecPath 可以指定要使用的可执行文件。

使用自定义的 execPath 启动的 Node.js 进程,会使用子进程的环境变量 NODE_CHANNEL_FD 中指定的文件描述符与父进程通信。

不像 POSIX 的 fork(2)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.
    • env <Object> Environment key-value pairs.
    • 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.

It is important to 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.