用NodeJS实现集群计算

Node的单个实例运行在单个的线程中,要充分利用多核系统,我们可以运行Node进程集群来处理负载。

也就是说,如果系统有8核,单个Node实例只能使用其中1核,可以利用cluster包的workers概念来充分利用所有的核。有趣的是,它们可以共享同一个端口。

该模块还处于实验阶段。

CODE:

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

if (cluster.isMaster) {
  // Fork workers.
  require('os').cpus().forEach(function(){
    cluster.fork();
  });
  // In case the worker dies!
  cluster.on('exit', function(worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died');
  });
  // As workers come up.
  cluster.on('listening', function(worker, address) {
    console.log("A worker with #"+worker.id+" is now connected to " +\
     address.address +\
    ":" + address.port);
  });
  // When the master gets a msg from the worker increment the request count.
  var reqCount = 0;
  Object.keys(cluster.workers).forEach(function(id) {
    cluster.workers[id].on('message',function(msg){
      if(msg.info && msg.info == 'ReqServMaster'){
        reqCount += 1;
      }
    });
  });
  // Track the number of request served.
  setInterval(function() {
    console.log("Number of request served = ",reqCount);
  }, 1000);

} else {
  // Workers can share the same port!
  require('http').Server(function(req, res) {
    res.writeHead(200);
    res.end("Hello from Cluster!");
    // Notify the master about the request.
    process.send({ info : 'ReqServMaster' });
  }).listen(8000);
}在一个4核的计算机上,输出如下:

CODE:

Number of request served =  0
A worker with #2 is now connected to 0.0.0.0:8000
A worker with #4 is now connected to 0.0.0.0:8000
A worker with #1 is now connected to 0.0.0.0:8000
A worker with #3 is now connected to 0.0.0.0:8000
Number of request served =  0
...
Number of request served =  2
..
Number of request served =  4
...
Number of request served =  6

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/301743/viewspace-746038/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/301743/viewspace-746038/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值