Skip to main content

utilityProcess

utilityProcess 创建一个启用了 Node.js 和消息端口的子进程。它提供了与 Node.js 中 child_process.fork API 等效的功能,但使用 Chromium 的 服务 API 来启动子进程。

进程: 主进程

方法

🌐 Methods

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

  • modulePath 字符串 - 指向应在子进程中作为入口点运行的脚本的路径。
  • args string[](可选)- 将作为 process.argv 在子进程中可用的字符串参数列表。
  • options 对象(可选)
    • env 对象(可选)- 环境键值对。默认值为 process.env
    • execArgv string[](可选)- 传递给可执行文件的字符串参数列表。
    • cwd 字符串(可选)- 子进程的当前工作目录。
    • stdio(string[] | string)(可选)- 允许配置子进程的 stdoutstderr 模式。默认值为 inherit。字符串值可以是 pipeignoreinherit 之一,关于这些值的更多详情可以参考 Node.js 的 标准输入输出 文档。目前此选项仅支持将 stdoutstderr 配置为 pipeinheritignore。将 stdin 配置为除 ignore 外的任何属性是不支持的,会导致错误。例如,支持的值将按如下方式处理:
      • pipe:等同于 ['ignore', 'pipe', 'pipe']
      • ignore:等同于 ['忽略', '忽略', '忽略']
      • inherit:相当于 ['ignore', 'inherit', 'inherit'](默认值)
    • serviceName 字符串(可选)——要显示在 app.getAppMetrics 返回的 ProcessMetricname 属性以及 appchild-process-gone 事件 中的进程名称。默认值为 Node Utility Process
    • allowLoadingUnsignedLibraries 布尔值(可选)macOS - 使用此标志时,实用程序进程将在 macOS 上通过 Electron Helper (Plugin).app 辅助可执行文件启动,该文件可以使用 com.apple.security.cs.disable-library-validationcom.apple.security.cs.allow-unsigned-executable-memory 权限进行代码签名。这将允许实用程序进程加载未签名的库。除非你特别需要此功能,否则最好保持此选项禁用。默认值为 false
    • disclaim 布尔值(可选)macOS - 使用此标志时,实用程序进程将放弃对子进程的责任。这将导致操作系统在处理透明度、同意和控制(TCC)等安全策略时,将子进程视为独立实体。当放弃责任时,子进程发起的任何 TCC 请求都不会归因于父进程。这在启动运行第三方或其他不受信任代码的进程时非常有用。默认值为 false
    • respondToAuthRequestsFromMainProcess 布尔值(可选)- 使用此标志,所有通过 net 模块 创建的 HTTP 401 和 407 网络请求都可以通过主进程中的 app#login 事件来响应,而不是默认的 login 事件在 ClientRequest 对象上。默认值为 false

返回 UtilityProcess

🌐 Returns UtilityProcess

note

utilityProcess.fork 只能在 App 上触发 ready 事件后才能调用。

类:UtilityProcess

🌐 Class: UtilityProcess

UtilityProcess 的实例表示带有 Node.js 集成的 Chromium 创建的子进程。

UtilityProcess 是一个 事件触发器

实例方法

🌐 Instance Methods

child.postMessage(message, [transfer])

  • message 任何
  • transfer MessagePortMain[](可选)

向子进程发送消息,可选择性地转移零个或多个 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
})
note

你可以使用 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().