Home > Web Front-end > JS Tutorial > Learning the cluster module in Node

Learning the cluster module in Node

不言
Release: 2018-06-30 14:23:53
Original
2108 people have browsed it

This article mainly introduces the cluster module of Node learning record. The content is quite good. I will share it with you now and give it as a reference.

In the context of today's machines with multi-core CPUs, Node's single-threaded design can no longer fully "squeeze" machine performance. So starting from v0.8, Node has added a built-in module - "cluster", hence the name. It can realize the cluster function by managing a bunch of child processes through a parent process.

var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length; // 获取CPU的个数
 
if (cluster.isMaster) {
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
 
  cluster.on(&#39;exit&#39;, function(worker, code, signal) {
    console.log(&#39;worker &#39; + worker.process.pid + &#39; died&#39;);
  });
} else {
  http.createServer(function(req, res) {
    res.writeHead(200);
    res.end("hello world\n");
  }).listen(8000);
}
Copy after login

Use the isMaster attribute to determine whether it is a Master process. If so, fork the child process, otherwise start a server. Each HTTP server can listen to the same port. However, in actual projects, our startup code is generally encapsulated in app.js. It is not elegant to embed the entire startup logic in the above if else. So, we can do this:

var cluster = require(&#39;cluster&#39;);
var numCPUs = require(&#39;os&#39;).cpus().length;
 
if (cluster.isMaster) {
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
  // 其它代码
  
} else {
  require("./app.js");
}
Copy after login

The simplicity is that the original application logic does not need to know whether it is in a cluster or unilaterally. (Of course, if the application maintains certain states in memory, such as sessions, it needs to use some mechanism to share them. I won’t go into details here)

The above is the entire content of this article. I hope it will be useful to everyone. Learning will be helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

nodejs method to implement bigpipe asynchronous loading of pages

About node.js system based on fs module Methods for reading and writing files and directories

The above is the detailed content of Learning the cluster module in Node. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template