I have a Node application creating a number of worker processes via fork()
to perform some tasks on their own. As a byproduct, the workers produce a snapshot of actions taken in the form of object. Each worker has an event listener attached to the 'exit' event, at which time they send their snapshot back to the parent process via process.send()
.
Here's an example of the set up:
// parent.js
const exec = require('child_process');
const worker = exec.fork('worker.js', [], {
stdio: ['pipe', 'pipe', null, 'ipc']
});
worker.on('message', (snapshot) => {
// Handle the snapshot sent from worker
});
// worker.js
process.on('exit', () => {
process.send({ /* snapshot data */ })
});
/* Arbitrary task work */
Is this an acceptable pattern for parent/worker IPC? Specifically, will this reliably about the reliability of receiving messages in the parent process given the nature of the exit
event and process.send()
?
Is the beforeExit
event better suited to this pattern?
Aucun commentaire:
Enregistrer un commentaire