utilityProcess
utilityProcess
crea un proceso hijo con los puertos de Node.js y Message habilitados. Este proporciona un equivalente al API child_process.fork
de Node.js, pero en su lugar usa el API Services de Chromium para lanzar el proceso hijo.
Process: Main
Métodos
utilityProcess.fork(modulePath[, args][, options])
- Cadena
modulePath
- Ruta al script que debe ejecutarse como punto de entrada del proceso hijo. args
string[] (opcional) - Listado de argumentos de cadena que deben estar disponibles comoprocess.argv
en el proceso hijo.
Devuelve UtilityProcess
Clase: UtilityProcess
Las instancias de
UtilityProcess
representan el proceso hijo generado por Chromium con la integración de Node.js.
UtilityProcess
es un EventEmitter.
Métodos de Instancia
child.postMessage(message, [transfer])
mensaje
cualquieratransfer
MessagePortMain[] (optional)
Envía un mensaje al proceso hijo, transfiriendo opcionalmente la propiedad de cero o más objetos MessagePortMain
.
Por ejemplo:
// 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()
Devuelve boolean
Finaliza el proceso con gracia. 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.
Propiedades de la instancia
child.pid
A Integer | undefined
representing the process identifier (PID) of the child process. If the child process fails to spawn due to errors, then the value is undefined
. When the child process exits, then the value is undefined
after the exit
event is emitted.
child.stdout
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
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.
Eventos de Instancia
Event: 'spawn'
Emitted once the child process has spawned successfully.
Event: 'exit'
Devuelve:
code
number - Contains the exit code for the process obtained from waitpid on posix, or GetExitCodeProcess on windows.
Emitted after the child process ends.
Evento: 'message'
Devuelve:
mensaje
cualquiera
Emitted when the child process sends a message using process.parentPort.postMessage()
.