Nodejs is a single thread, which means node can only work with one processor. But most servers have multiple cores. Fortunately Nodejs provides a cluster module that can assign tasks to child processes. Each subprocess has some special capabilities, such as the ability to share a socket connection with other child processes. When using cluster, the master process does not participate in each specific transaction, the master process manages the child processes, but when the child processes interact with I/O operations, they are direct processes and do not need to pass through the main process.
A simple example:
varCluster = require (' cluster '));varHTTP = require (' http ');varNumcpus = require (' OS '). CPUs (). length;if(cluster.ismaster) {//Create a worker process for(vari=0; i<numcpus; i++) {cluster.fork (); } cluster.on (' Death ',function(worker) {Console.log (' Worker ' + worker.pid + ' died '); }); }Else{ //worker process Create HTTP servicehttp. Server (function(req, res) {Res.writehead (200); Res.end ("Hello world\n"); }). Listen (8000);}
The principle of cluster work is that each node process is either a master process or a worker process. When a master process calls the Cluster.fork () method, it creates a child process that is exactly the same as the main process, except for two properties that allow each process to check itself as a parent/child process. Cluster.ismaster returns true in the main process, while Cluster.isworker () returns FALSE, in the main process.
In addition to sharing the socket, you can also use cluster to do more things, because it is based on the Child_process module, which provides a series of properties, the most useful of which can check the child process health state, in the above example, when the child process died, The main process uses console.log () to output the death process, and since the death process is monitored, we can recreate a new child process when the child process dies.
function (worker) { console.log (' worker ' + worker.pid + ' died '); Cluster.fork ();});
This simple improvement allows the main process to restart the dead process, ensuring that all CPUs have our servers running. There are more things you can do because a worker process can send messages to the main process, so that each worker process can report its own state, such as memory usage. This allows the main process to detect which worker processes have become unstable, which processes are not frozen, or blocked.
varCluster = require (' cluster '));varHTTP = require (' http ');varNumcpus = require (' OS '). CPUs (). length;varRsswarn = (12*1024*1024), Heapwarn= (12*1024*1024);if(cluster.ismaster) {//Create a worker process for(vari=0; i<numcpus; i++) { varWorker =cluster.fork (); Worker.on (' Message ',function(m) {if(m.memory) {if(M.memory.rss >Rsswarn) {Console.log (' Worker ' + m.process + ' using too much momory. '); }}})} Cluster.on (' Death ',function(worker) {Console.log (' Worker ' + worker.pid + ' died '); Cluster.fork (); }); }Else{ //worker process Create HTTP servicehttp. Server (function(req, res) {Res.writehead (200); Res.end ("Hello world\n"); }). Listen (8000); //status reported once per secondSetInterval (functionReport () {process.send ({memory:process.memoryUsage (), process:process.pid}); })}
In this example, the worker process reports its own memory usage, and when the child process uses too much memory, the main process sends a warning to the log. This allows the node master process to have the ability to control, but also bring benefits. This messaging interface also allows the main process to send messages back to the worker process. Of course, we can also do more with message delivery.
Nodejs: Using multiple processors