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
。serialization
<string> 指定用于在进程之间发送消息的序列化类型。 可能的值为'json'
和'advanced'
。 详见高级序列化。 默认值:'json'
。silent
<boolean> 如果为true
,则子进程的 stdin、stdout 和 stderr 会被 pipe 到父进程,否则它们会继承自父进程,详见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, seeoptions.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
.serialization
<string> Specify the kind of serialization used for sending messages between processes. Possible values are'json'
and'advanced'
. See Advanced serialization for more details. Default:'json'
.silent
<boolean> Iftrue
, 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 forchild_process.spawn()
'sstdio
for more details. Default:false
.stdio
<Array> | <string> Seechild_process.spawn()
'sstdio
. When this option is provided, it overridessilent
. 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 (seesetuid(2)
).gid
<number> Sets the group identity of the process (seesetgid(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.