Multiple instances of node.js on different cores

后端 未结 2 1765
北海茫月
北海茫月 2021-01-31 13:13

I would like to set up 4 different node.js instances, each on their own core. Does node.js stack new instances on the same core, or set them on new cores also?

The insta

2条回答
  •  星月不相逢
    2021-01-31 13:25

    try this:

    var cluster = require('cluster')
      , 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.pid + ' died');
      });
    
      return;
    }
    

    complete code here

    also, look here

提交回复
热议问题