Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Killing node.js workers after function is done

I'm a total node.js newbie who's just started tinkering with it. I have a piece of code that executes a function that processes strings on all cpu cores, and I wish to determine which worker completed the function first by it's id, and after that kill every worker (or just exit node).

Here's the simplified code of my program:

var cluster = require('cluster'),
    cpus = require("os").cpus().length, // 4 cores
    myArray = ["foo","bar","baz","qux"]; // 1 string per core

if (cluster.isMaster) {
    for (var i = 0; i < cpus; i++) {
        var worker = cluster.fork();
    }
    cluster.on('exit', function(worker, code, signal) {
        console.log('worker ' + worker.process.pid + ' died');
    });
} else if (cluster.isWorker) {
    computeString(myArray[cluster.worker.id]);
}

function computeString() {
    // Code to compute...
}

This code works, and the computeString() function finishes much faster than executing it outside the

else if (cluster.isWorker) {}

So the problem is that after one worker/process completes the function, node waits until every process has done their job and doesn't terminate after those either, every process stay idle until I hit ctrl+c.

My approach was:

function computeString() {
    // Code to compute...
    if (done) {
         console.log("Worker #" + cluster.worker + " completed the task!");
         for (var id in cluster.workers) {
            cluster.workers[id].kill();
         }
     }
}

But since I'm asking here, it obviously doesn't work :)

like image 643
fnx Avatar asked Mar 21 '13 13:03

fnx


People also ask

How do you stop a worker thread in node JS?

Using setTimeout() to call worker. terminate() on yourself will NOT get called until your worker thread gets back to the event loop to process the timer event from the setTimeout() .

How do you kill a Processin node JS?

kill() Method. The process. kill( pid[,signal] ) is an inbuilt method of node. js which sends a signal to the process, pid (which is the process id) and signal is in the string format that is the signal to send.

Which shortcut command is used to kill a process in node JS?

Method 1: Using the Ctrl+C key This shortcut can be used to stop any running process by hitting this command on terminal.

When should I use node workers?

In Node. js, worker threads come handy when doing large JavaScript tasks. Worker makes it simple to run Javascript code in parallel using threads, making it considerably faster and more efficient. We can complete difficult jobs without disrupting the main thread.


1 Answers

So you want to kill all workers when the first worker has done its work?

...
cluster.on('exit', function(worker, code, signal) {
  console.log('worker ' + worker.process.pid + ' died');
  // kill the other workers.
  for (var id in cluster.workers) {
    cluster.workers[id].kill();
  }
  // exit the master process
  process.exit(0);
});
...
function computeString() {
  // Code to compute...
  if (done) {
    process.exit(0); // exit the worker process cleanly, triggering the 'exit' event
  }
};
like image 152
robertklep Avatar answered Sep 20 '22 21:09

robertklep