Answer the question
In order to leave comments, you need to log in
How to stop a process in node.js?
Good day to all.
There is a master node process that runs several children. Each of the children is a parser that receives data from the master. Everything works fine, but I noticed one feature - when the parsing is finished, the child process is not closed. I struggled with the problem for a day and found out that the node does not close if there are unfinished handlers on the stack. I began to dig what I have hanging in the stack so far. And I found that this is the handler for incoming messages from the master process:
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
let worker = cluster.fork();
worker.send(i);
}
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} has finished his job.`);
numberOfProcesses--;
if(numberOfProcesses <= 0) {
console.log(`Parsing was completed.`);
process.exit(0);
}
});
} else {
process.once('message', msg => {
// именно вот из-за этого обработчика процесс продолжает работать
console.log(process.pid, msg)
})
}
Answer the question
In order to leave comments, you need to log in
We will still have to offer process.exit ()
Handlers are hung on the process, so it is not going to end itself.
You need to call after the completion of all tasks - Promise to help.
It is unlikely to succeed otherwise.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question