Multiple instances of node.js on different cores

后端 未结 2 1762
北海茫月
北海茫月 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

    0 讨论(0)
  • 2021-01-31 13:39

    In general the system will try to do it this own, to maximize utilization of cpu. However if you want to target a particular CPU, you should check out TaskSet. It set's the affinity of the process.

    Also there are several useful questions that discuss the same topic. Have a look.

    1. Upstart: each process on different core Nodejs

    2. Node.js - targeting a cpu core

    3. Node.js on multi-core machines

    4. In AmazonEC2 cpu core and nodejs

    5. How to deploy Node.js in cloud for high availability using multi-core

    6. Multi node modlue to utilize cpu

    There is also a module, Cluster, that can also be very useful for CPU utilization. It let's you fork multiple processes to distribute load to multiple cores.


    UPDATE


    Finally, I have deployed something similar according to OP.


    Case 1.


    1. I have a dedicated sever with 8 cores of cpu.
    2. I have deployed a single node thread and used the Cluster module to share the work load (As Described by pl47ypus PS: thanks for his answer ).
    3. The result is good, but some times the child thread may become unresponsive, So I have decided to try the old process, which I had used in my previous application.

    Case 2.


    1. Same server, I have deployed 8 processes of node.js with different ports.
    2. Then I put nginx in front of these, listening on port 80 with worker process 8.
    3. Result is best than case 1 and also its very easy to configure nginx , its most stable too.

    I also suggest you just try some of the solutions mentioned and keep monitoring your system in each case; like CPU, memory usage and io. Finally, from your tests you will see the best solution for your use case. Every application has its own requirements, so its better to try and find what your applications real need.

    0 讨论(0)
提交回复
热议问题