utilityProcess
utilityProcess
创建一个启用 Node.js 和消息端口的子进程。它提供了与 Node.js 中的 child_process.fork
API 等效的功能,但使用 Chromium 中的 服务 API 来启动子进程。
¥utilityProcess
creates a child process with
Node.js and Message ports enabled. It provides the equivalent of child_process.fork
API from Node.js
but instead uses Services API from Chromium to launch the child process.
进程:主进程
¥Process: Main
方法
¥Methods
utilityProcess.fork(modulePath[, args][, options])
-
modulePath
字符串 - 应作为子进程中的入口点运行的脚本的路径。¥
modulePath
string - Path to the script that should run as entrypoint in the child process. -
args
字符串[](可选) - 将在子进程中用作process.argv
的字符串参数列表。¥
args
string[] (optional) - List of string arguments that will be available asprocess.argv
in the child process.
¥Returns UtilityProcess
类:UtilityProcess
¥Class: UtilityProcess
UtilityProcess
的实例代表 Chromium 通过 Node.js 集成生成的子进程。¥Instances of the
UtilityProcess
represent the Chromium spawned child process with Node.js integration.
UtilityProcess
是 EventEmitter。
¥UtilityProcess
is an EventEmitter.
实例方法
¥Instance Methods
child.postMessage(message, [transfer])
-
message
任意¥
message
any -
transfer
MessagePortMain[](可选)¥
transfer
MessagePortMain[] (optional)
向子进程发送消息,可选择转让零个或多个 MessagePortMain
对象的所有权。
¥Send a message to the child process, optionally transferring ownership of
zero or more MessagePortMain
objects.
例如:
¥For example:
// Main process
const { port1, port2 } = new MessageChannelMain()
const child = utilityProcess.fork(path.join(__dirname, 'test.js'))
child.postMessage({ message: 'hello' }, [port1])
// Child process
process.parentPort.once('message', (e) => {
const [port] = e.ports
// ...
})
child.kill()
返回 boolean
¥Returns boolean
优雅地终止进程。在 POSIX 上,它使用 SIGTERM 但会确保进程在退出时被收获。如果杀死成功,该函数返回 true,否则返回 false。
¥Terminates the process gracefully. On POSIX, it uses SIGTERM but will ensure the process is reaped on exit. This function returns true if the kill is successful, and false otherwise.
实例属性
¥Instance Properties
child.pid
Integer | undefined
代表子进程的进程标识符 (PID)。如果子进程由于错误而未能生成,则该值为 undefined
。当子进程退出时,发出 exit
事件后该值为 undefined
。
¥A Integer | undefined
representing the process identifier (PID) of the child process.
If the child process fails to spawn due to errors, then the value is undefined
. When
the child process exits, then the value is undefined
after the exit
event is emitted.
child.stdout
代表子进程的标准输出的 NodeJS.ReadableStream | null
。如果子进程是在 options.stdio[1] 设置为 'pipe' 以外的任何值的情况下生成的,那么这将是 null
。当子进程退出时,发出 exit
事件后该值为 null
。
¥A NodeJS.ReadableStream | null
that represents the child process's stdout.
If the child was spawned with options.stdio[1] set to anything other than 'pipe', then this will be null
.
When the child process exits, then the value is null
after the exit
event is emitted.
// Main process
const { port1, port2 } = new MessageChannelMain()
const child = utilityProcess.fork(path.join(__dirname, 'test.js'))
child.stdout.on('data', (data) => {
console.log(`Received chunk ${data}`)
})
child.stderr
代表子进程的 stderr 的 NodeJS.ReadableStream | null
。如果子进程是在 options.stdio[2] 设置为 'pipe' 以外的任何值的情况下生成的,那么这将是 null
。当子进程退出时,发出 exit
事件后该值为 null
。
¥A NodeJS.ReadableStream | null
that represents the child process's stderr.
If the child was spawned with options.stdio[2] set to anything other than 'pipe', then this will be null
.
When the child process exits, then the value is null
after the exit
event is emitted.
实例事件
¥Instance Events
事件:'spawn'
¥Event: 'spawn'
子进程成功生成后发出。
¥Emitted once the child process has spawned successfully.
事件:'exit'
¥Event: 'exit'
返回:
¥Returns:
-
code
数字 - 包含从 posix 上的 waitpid 或 Windows 上的 GetExitCodeProcess 获取的进程的退出代码。¥
code
number - Contains the exit code for the process obtained from waitpid on posix, or GetExitCodeProcess on windows.
子进程结束后发出。
¥Emitted after the child process ends.
事件:'message'
¥Event: 'message'
返回:
¥Returns:
-
message
任意¥
message
any
当子进程使用 process.parentPort.postMessage()
发送消息时发出。
¥Emitted when the child process sends a message using process.parentPort.postMessage()
.