How to use cluster sails (to use more cores)?

I am trying to run the sails.js application with the cluster library to allow it to run more processes to use all the kernels of my machine, but somehow the method used with the expression does not work here.

 var cluster = require('cluster'), numCPUs = require('os').cpus().length; // create the server if (cluster.isMaster) { for (var i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('exit', function(worker, code, signal) { console.log('worker %d died (%s). restarting...', worker.process.pid, signal || code); cluster.fork(); }); } else { // Start sails and pass it command line arguments require('sails').lift(require('optimist').argv); console.log("Sails forked"); } 

After that, I just run it through:

 $ sails lift 

but I get the same results when I run apache bench for this performance testing. Any help? Did I miss something?

EDIT

Even when I put console.log in cluster.fork , if part, then I do not get output.

+6
source share
2 answers

Finally, I used the pm2 module for this, as mentioned in one of the sails problems https://github.com/balderdashy/sails/issues/170

First you need to install it:

 $ npm install -g pm2 

and then run the script:

 $ pm2 start app.js -i max 

then you can control it with:

 $ pm2 monit 

enter image description here

or list the process:

 $ pm2 l 

enter image description here

+12
source

Try this and see what your result is (very simplified code for debugging):

 var cluster = require('cluster'), numCPUs = require('os').cpus().length; console.log('global '+numCPUs); // create the server if (cluster.isMaster) { console.log('master '+numCPUs); for (var i = 0; i < numCPUs; i++) { console.log('this is for CPU '+i); cluster.fork(); console.log('just for giggles '+i); } } else { console.log('This is a worker!'); } 

... presumably you should get an output from all these calls. If everything is as expected, start adding your code back in half, see if it breaks and where, or if it starts to work.

If you miss a conclusion or something unexpected, and you still don't know what to do next, post your results here.

Here is an example of the expected output from my dev server:

 global 8 master 8 this is for CPU 0 just for giggles 0 this is for CPU 1 just for giggles 1 this is for CPU 2 just for giggles 2 this is for CPU 3 just for giggles 3 this is for CPU 4 just for giggles 4 this is for CPU 5 just for giggles 5 this is for CPU 6 just for giggles 6 this is for CPU 7 just for giggles 7 global 8 This is a worker! global 8 This is a worker! global 8 This is a worker! global 8 global 8 This is a worker! This is a worker! global 8 This is a worker! global 8 This is a worker! global 8 This is a worker! 

... which shows a couple of interesting things, in fact, if you are not very familiar with the cluster API or just want to see it in action, in particular that cluster.fork() is an asynchronous call (i.e. it will not be executed until the next cycle, therefore, why you do not start to see how the workers declare themselves until they are all bifurcated), but this child process, once generated, will be executed in parallel (therefore, doubling numbers global 8 and This is a worker! messages in the middle).

0
source

All Articles