0

Possible Duplicate:
node.js on multi-core machines

Since node.js makes use of single thread model, how can node.js leverage multiple cores? Without using multiple cores, I think the usage of CPU is not enough, am I right?

Community
  • 1
  • 1
Adam Lee
  • 24,710
  • 51
  • 156
  • 236

1 Answers1

1

you can use the core cluster module

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('exit', function(worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died');
  });
} else {
  // Workers can share any TCP connection
  // In this case its a HTTP server
  http.createServer(function(req, res) {
    res.writeHead(200);
    res.end("hello world\n");
  }).listen(8000);
}
zemirco
  • 16,171
  • 8
  • 62
  • 96