Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many child_processes should I fork() in node.js?

My question is quite simple. though, it may require different variable to be answered (i guess)

I'm playing around with node.js and I'm thinking of how to use it in a multi core architecture.

Latest version provides child_process.fork()and child.spawn() methods for multi-process programming. I've read on this very good (but dated) article about using Node.js as a large-scale Comet server. Now then nodejs provides multi-process programming, I really have no idea about how many processes should I have to spawn for serving large number of request (assuming my server is running on just one machine). Is there a way of choosing the 'best' (or at least a good) number of child processes doing the same job?

Any link to a starting guide would be very appreciated.

Thank you

like image 677
ArtoAle Avatar asked Nov 06 '11 15:11

ArtoAle


People also ask

How many child process can I create in node JS?

There are four different ways to create a child process in Node: spawn() , fork() , exec() , and execFile() .

How many child processes can a process have?

Answer. The process tree can only display up to 15 child processes; either 15 unsuppressed, 15 suppressed, or 15 of both types.

How many threads does Node use?

Conclusion. Node. js has two types of threads: one Event Loop and k Workers.


1 Answers

The Node.js official documentation has an example of how to use cluster:

var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('death', function(worker) {
    console.log('worker ' + worker.pid + ' died');
  });
} else {
  // Worker processes have a http server.
  http.Server(function(req, res) {
    res.writeHead(200);
    res.end("hello world\n");
  }).listen(8000);
}

As you can see in the code above, you should use as many forks as the number of cpus you have, so that all cores work in the same time (distribute the work between your processor's cores).

like image 153
alessioalex Avatar answered Oct 14 '22 11:10

alessioalex