跳到主要内容

utilityProcess

utilityProcess 创建一个子进程,该进程启用了 Node.js 和 Message ports。它提供了 Node.js 中 child_process.fork API 的等效功能,但改为使用 Chromium 的 Services API 来启动子进程。

进程: 主进程

方法

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

  • modulePath string - 将作为子进程入口点运行的脚本路径。
  • args string[] (可选) - 将在子进程中作为 process.argv 可用的字符串参数列表。
  • options Object (可选)
    • env Object (可选) - 环境变量键值对。 默认值为 process.env
    • execArgv string[] (可选) - 传递给可执行文件的字符串参数列表。
    • cwd string (可选) - 子进程的当前工作目录。
    • stdio (string[] | string) (可选) - 允许配置子进程的 stdoutstderr 模式。 默认值为 inherit。 字符串值可以是 pipeignoreinherit 中的一个,有关这些值的更多详细信息,请参阅 Node.js 中的 stdio 文档。 目前,此选项仅支持将 stdoutstderr 配置为 pipeinheritignore。 不支持将 stdin 配置为 ignore 以外的任何属性,否则将导致错误。 例如,支持的值将按以下方式处理:
      • pipe:等同于 ['ignore', 'pipe', 'pipe']
      • ignore:等同于 ['ignore', 'ignore', 'ignore']
      • inherit:等同于 ['ignore', 'inherit', 'inherit'] (默认值)
    • serviceName string (可选) - 将出现在 ProcessMetricname 属性中的进程名称,该属性由 app.getAppMetricsappchild-process-gone 事件返回。 默认值为 Node Utility Process
    • allowLoadingUnsignedLibraries boolean (可选) macOS - 使用此标志,utility process 将通过 macOS 上的 Electron Helper (Plugin).app 助手可执行文件启动,该文件可以使用 com.apple.security.cs.disable-library-validationcom.apple.security.cs.allow-unsigned-executable-memory 授权进行代码签名。这将允许 utility process 加载未签名的库。除非您特别需要此功能,否则最好将其禁用。 默认值为 false
    • respondToAuthRequestsFromMainProcess boolean (可选) - 使用此标志,通过 net 模块创建的所有 HTTP 401 和 407 网络请求将允许通过主进程中的 app#login 事件而不是 login 事件来响应 ClientRequest 对象。 默认值为 false

返回 UtilityProcess

注意: utilityProcess.fork 只能在 App 发出 ready 事件后调用。

类:UtilityProcess

UtilityProcess 的实例代表了集成 Node.js 的 Chromium 生成的子进程。

UtilityProcess 是一个 EventEmitter

实例方法

child.postMessage(message, [transfer])

  • message any
  • transfer MessagePortMain[] (可选)

向子进程发送一条消息,可以选择性地转移零个或多个 MessagePortMain 对象的拥有权。

例如

// 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

正常终止进程。在 POSIX 上,它使用 SIGTERM,但会确保在退出时回收进程。如果终止成功,此函数返回 true,否则返回 false。

实例属性

child.pid

一个 Integer | undefined,表示子进程的进程标识符 (PID)。在子进程成功生成之前,该值为 undefined。当子进程退出时,在发出 exit 事件后,该值为 undefined

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,表示子进程的 stdout。如果子进程以 options.stdio[1] 设置为 'pipe' 以外的任何值启动,则此值为 null。当子进程退出时,在发出 exit 事件后,该值为 null

// 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,表示子进程的 stderr。如果子进程以 options.stdio[2] 设置为 'pipe' 以外的任何值启动,则此值为 null。当子进程退出时,在发出 exit 事件后,该值为 null

实例事件

事件:'spawn'

子进程成功生成后发出。

事件:'error' 实验性

返回

  • type string - 错误类型。以下值之一:
    • FatalError
  • location string - 错误源位置。
  • report string - Node.js 诊断报告

当子进程由于 V8 的不可继续错误而需要终止时发出。

无论您是否监听 error 事件,在子进程终止后都会发出 exit 事件。

事件:'exit'

返回

  • code number - 包含从 POSIX 上的 waitpid 或 Windows 上的 GetExitCodeProcess 获取的进程退出代码。

子进程结束后发出。

事件:'message'

返回

  • message any

当子进程使用 process.parentPort.postMessage() 发送消息时发出。