utilityProcess
utilityProcess creates a child process with Node.js and Message ports enabled. It provides the equivalent of child_process.fork API from Node.js but instead uses Services API from Chromium to launch the child process.
Process: Main
Methoden
utilityProcess.fork(modulePath[, args][, options])
modulePathstring - Path to the script that should run as entrypoint in the child process.argsstring[] (optional) - List of string arguments that will be available asprocess.argvin the child process.
Returns UtilityProcess
utilityProcess.fork can only be called after the ready event has been emitted on App.
Class: UtilityProcess
Instances of the
UtilityProcessrepresent the Chromium spawned child process with Node.js integration.
UtilityProcess is an EventEmitter.
Beispiel Methoden
child.postMessage(message, [transfer])
messageanytransferMessagePortMain[] (optional)
Send a message to the child process, optionally transferring ownership of zero or more MessagePortMain objects.
Ein Beispiel:
// 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()
Gibt boolean zurück
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.
Instanz Eigenschaften
child.pid
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] You can use the
pidto determine if the process is currently running.
child.stdout
A NodeJS.ReadableStream | null that represents the child process's stdout. When the child process exits, then the value is null after the exit event is emitted. If the child was spawned with options.stdio[1] set to anything other than 'pipe', then this will be 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 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. If the child was spawned with options.stdio[1] set to anything other than 'pipe', then this will be null.
Instanz-Ereignisse
Event: 'spawn'
Emitted once the child process has spawned successfully.
Event: 'error' Experimental
Kehrt zurück:
typestring - Type of error. Einer der folgenden Werte:FatalError
locationstring - Source location from where the error originated.reportstring -Node.js diagnostic report.
Emitted when the child process needs to terminate due to non continuable error from V8.
No matter if you listen to the error event, the exit event will be emitted after the child process terminates.
Event: 'exit'
Kehrt zurück:
codenumber - Contains the exit code for the process obtained from waitpid on POSIX, or GetExitCodeProcess on Windows.
Emitted after the child process ends.
Event: 'message'
Kehrt zurück:
messageany
Emitted when the child process sends a message using process.parentPort.postMessage().