utilityProcess
utilityProcess 创建一个启用了 Node.js 和消息端口的子进程。它提供了与 Node.js 中 child_process.fork API 等效的功能,但使用 Chromium 的 服务 API 来启动子进程。
进程: 主进程
方法
🌐 Methods
utilityProcess.fork(modulePath[, args][, options])
modulePath字符串 - 指向应在子进程中作为入口点运行的脚本的路径。argsstring[](可选)- 将作为process.argv在子进程中可用的字符串参数列表。
🌐 Returns UtilityProcess
utilityProcess.fork 只能在 App 上触发 ready 事件后才能调用。
类:UtilityProcess
🌐 Class: UtilityProcess
UtilityProcess的实例表示带有 Node.js 集成的 Chromium 创建的子进程。
UtilityProcess 是一个 事件触发器。
实例方法
🌐 Instance Methods
child.postMessage(message, [transfer])
message任何transferMessagePortMain[](可选)
向子进程发送消息,可选择性地转移零个或多个 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.
Until the child process has spawned successfully, the value is undefined. When
the child process exits, then the value is undefined after the exit event is emitted.
const child = utilityProcess.fork(path.join(__dirname, 'test.js'))
console.log(child.pid) // undefined
child.on('spawn', () => {
console.log(child.pid) // Integer
})
child.on('exit', () => {
console.log(child.pid) // undefined
})
你可以使用 pid 来判断该进程当前是否正在运行。
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
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
Event: 'spawn'
子进程成功生成后发出。
🌐 Emitted once the child process has spawned successfully.
Event: 'error' Experimental
返回:
🌐 Returns:
type字符串 - 错误类型。以下值之一:FatalError
location字符串 - 错误来源的位置。report字符串 -Node.js diagnostic report。
当子进程由于 V8 的不可继续错误而需要终止时发出。
🌐 Emitted when the child process needs to terminate due to non continuable error from V8.
无论你是否监听 error 事件,子进程终止后都会触发 exit 事件。
🌐 No matter if you listen to the error event, the exit event will be emitted after the
child process terminates.
Event: 'exit'
返回:
🌐 Returns:
code数字 - 包含通过 POSIX 的 waitpid 或 Windows 的 GetExitCodeProcess 获得的进程退出代码。
子进程结束后发出。
🌐 Emitted after the child process ends.
Event: 'message'
返回:
🌐 Returns:
message任何
当子进程使用 process.parentPort.postMessage() 发送消息时触发。
🌐 Emitted when the child process sends a message using process.parentPort.postMessage().