Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

深入浅出 Node.js 里关于集群一段代码的问题

$
0
0

在 249 页的自动重启的代码,master 程序的代码如下:

var fork = require('child_process').fork;
var ncpus = require('os').cpus();
var net = require('net');

var server = net.createServer();
server.listen(3000);

var workers = {};
var createWorker = function() {
  var worker = fork('./worker.js');
  worker.on('exit', function() {
    console.log('Worker ' + worker.pid + ' exited.');
    delete worker[worker.pid];
    createWorker();
  });
  worker.send('server', server);
  workers[worker.pid] = worker;
  console.log('Created worker. pid: ' + worker.pid);
};

for (var i = 0; i < 1; i++) {
  createWorker();
}

process.on('exit', function() {
  for (var pid in workers) {
    workers[pid].kill();
  }
});

我的疑问是,这段代码中主程序也在监听端口,但是没有做出反应,如果请求交给主程序处理的时候,会一直没返回数据,这要怎么解决呢?


Viewing all articles
Browse latest Browse all 14821

Trending Articles