utilityProcess
utilityProcess は、Node.js と Message ポートが有効な子プロセスを作成します。 Node.js の child_process.fork API 相当の機能を提供しますが、代わりに Chromium の Services API で子プロセスを起動します。
プロセス: メイン
メソッド
utilityProcess.fork(modulePath[, args][, options])
modulePathstring - 子プロセスのエントリポイントとして起動されるスクリプトへのパスです。argsstring[] (任意) - その子プロセスでprocess.argvとして利用可能な引数の文字列リストです。
戻り値 UtilityProcess
utilityProcess.fork は、app で ready イベントが発生した後にのみ呼び出すことができます。
クラス: UtilityProcess
UtilityProcessのインスタンスは、Node.js を統合した Chromium が生成した子プロセスを表します。
UtilityProcess は EventEmitter を継承しています。
インスタンスメソッド
child.postMessage(message, [transfer])
messageanytransferMessagePortMain[] (任意)
メッセージをその子プロセスに送信し、任意でゼロ個以上の MessagePortMain オブジェクトの所有権を転送します。
以下がその例です。
// メインプロセス
const { port1, port2 } = new MessageChannelMain()
const child = utilityProcess.fork(path.join(__dirname, 'test.js'))
child.postMessage({ message: 'hello' }, [port1])
// 子プロセス
process.parentPort.once('message', (e) => {
const [port] = e.ports
// ...
})
child.kill()
戻り値 boolean
プロセスを正常終了させます。 POSIX では SIGTERM を使用しますが、アプリ終了時にはプロセスを刈り取ります。 この関数は kill が成功した場合に 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 型で、その子プロセスの標準出力を表します。 options.stdio[1] を 'pipe' 以外に設定して子プロセスを生成した場合、これは null になります。 子プロセスが終了すると、exit イベントが発生し、以降この値は null になります。
// メインプロセス
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 になります。
インスタンスイベント
イベント: 'spawn'
その子プロセスが正常に生成されると発生します。
イベント: 'error' 実験的
戻り値:
typestring - エラーの種別。 以下の値のいずれかです。FatalError
locationstring - エラーが発生したソースの位置。reportstring -Node.js 診断レポート。
V8 からの継続不可能なエラーにより子プロセスを終了する必要がある場合に発生します。
error イベントのリッスンに関係なく、子プロセスが終了し、exit イベントが発生します。
イベント: 'exit'
戻り値:
codenumber - プロセスの終了コードを格納しており、POSIX では waitpid、Windows では GetExitCodeProcess から得られるものです。
その子プロセスが終了した後に発生します。
イベント: 'message'
戻り値:
messageany
その子プロセスが process.parentPort.postMessage() でメッセージを送信したときに発生します。
イベント: 'login'
戻り値:
authenticationResponseDetailsObjecturlURLpidnumber
authInfoObjectisProxybooleanschemestringhoststringportIntegerrealmstring
callbackFunctionusernamestring (任意)passwordstring (optional)
Emitted when the utility process encounters an HTTP 401 or 407 authentication challenge, if the process was created with both respondToAuthRequestsFromMainProcess: true and a session option. The callback should be called with credentials to respond to the challenge. Calling callback without arguments will cancel the request.
This behaves the same as the login event on app but is scoped to the individual utility process instance.
const { session, utilityProcess } = require('electron')
const ses = session.defaultSession
const child = utilityProcess.fork('./worker.js', [], {
session: ses,
respondToAuthRequestsFromMainProcess: true
})
child.on('login', (authenticationResponseDetails, authInfo, callback) => {
callback('username', 'password')
})