跳转到主内容

utilityProcess

utilityProcess 使用 Node.js 和 Message 端口创建了一个子进程。 它提供一个相当于 Node.js 的 child_process.fork API,但使用 Chromium 的 Services API 代替来执行子进程。

进程:主进程

方法

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

  • modulePath string - 作为子进程执行入口的脚本文件路径。
  • args string[] (可选) - 字符串参数列表,在子进程中可以使用 process.argv
  • options Object (可选)
    • env Object (可选) - 环境变量 key-value 键值对。 默认值为 process.env.
    • execArgv string[] (可选) - 传递给执行文件的字符串参数列表。
    • cwd string (可选) - 子进程的工作目录。
    • stdio (string[] | string) (可能) - 允许配置子进程的 stdoutstderr 的模式。 默认值为 inherit. 字符串的值可以是 pipeignoreinherit 期中的一个,更多详细信息,可以参考 Node.js 的 stdio 文档。 当前此选项仅支持 stdoutstderr 配置为 pipeinheritignore。 Configuring stdin to any property other than ignore is not supported and will result in an error. 例如,支持的值将被处理如下:
      • pipe: equivalent to ['ignore', 'pipe', 'pipe']
      • ignore: equivalent to ['ignore', 'ignore', 'ignore']
      • inherit: equivalent to ['ignore', 'inherit', 'inherit'] (the default)
    • serviceName string (optional) - Name of the process that will appear in name property of ProcessMetric returned by app.getAppMetrics and child-process-gone event of app. 默认值为 Node Utility Process.
    • allowLoadingUnsignedLibraries boolean (可选) macOS - 使用这个标记,在 macOS 中执行 utility process 将通过 Electron Helper (Plugin).app 助手,它可以被 com.apple.security.cs.disable-library-validationcom.apple.security.cs.allow-unsigned-executable-memory 权限签名。 将允许 utility process 加载未签名的库。 除非您特别需要这种能力,否则最好保留这个禁用。 默认值为 false.

返回 UtilityProcess

Class: UtilityProcess

UtilityProcess 的实例代表 Chromium 派生子进程与 Node.js 的结合。

UtilityProcess 是一个 EventEmitter

实例方法

child.postMessage(message, [transfer])

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

Send a message to the child process, optionally transferring ownership of zero or more MessagePortMain objects.

例如:

// 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 代表子进程的进程 ID (PID) 。 如果由于子进程失败引发错误,则值为 undefined 。 当子进程退出,在 exit 事件处罚之后,值为 undefined

child.stdout

一个 NodeJS.ReadableStream | null 代表子进程的 stdout 。 If the child was spawned with options.stdio[1] set to anything other than 'pipe', then this will be 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

A NodeJS.ReadableStream | null 代表子进程的 stderr 。 If the child was spawned with options.stdio[2] set to anything other than 'pipe', then this will be null. 当子进程退出,在 exit 事件处罚之后,值为 null

实例事件

Event: 'spawn'

子进程成功生成后触发一次。

Event: 'exit'

返回:

  • code number - 包含进程的退出代码,posix 来自 waitpid,windows 来自 GetExitCodeProcess 。

进程结束之后触发。

Event: 'message'

返回:

  • message any

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