Node.js In the introduction and use of multi-process module Cluster


We all know that NodeJS’s largest feature is a single process, no blocking operation, and is a asynchronous event driven. These features of NodeJS can solve some problems well, for example, in server development, concurrency request processing is a big problem, and blocking functions can lead to resource waste and time delay. Through event registration, asynchronous functions, developers can improve resource utilization, and performance will improve. Since Node.js uses a single process, single-thread mode, how do NodeJs with a single core performance in today’s multi-core hardware? How to use multi-core CPUs? The founder Ryan Dahl recommends that runs multiple NodeJS processes, using certain communication mechanisms to coordinate tasks. Currently, there have been many third-party Node.js multi-process support modules release, and NodeJS 0.6.x or more provides a Cluster module that allows you to create a set of processes for sharing the same socket. .

This article describes the programming of Node.js under the multi-core CPU based on the Cluster module.

Cluster module introduction

The Cluster module provided by NodeJS is currently in the test phase. We can see the release information of the module on the official document of V0.10.7. :

STABILITY: 1 – Experimental
About the function of the module, the source document describes this “a SINGLE INSTANCE Of Node Runs in a Single Thread. To take advantage of multi-core systems the user willsometimes want to launch a cluster of node processes to handle the loading.That is: The example of Node is run in a single process, and sometimes to make full use of the multi-core system’s resource users to run a set of Node processes to assume the load.
First post a section of the module sample application code, next to the detailed analysis, the code is as follows:

VAR Cluster = Require (‘cluster’); var http = request (‘http’); var number = Require (‘OS’). CPUS (). Length; if (Cluster.ismaster) {Require ‘OS’). CPUS (). foreach (function () {cluster.Fork ();}); Cluster.on (‘exit’, code, sign, {console.log (‘worker’ + worker) .Process.PID + ‘Died’);}; Cluster.on (‘Listening’, Function (Worker, Address) {Console.log (“A Worker with #” + + “Is Now Connected to” + AddRESS .address + “:” + address.port);};} else {http.createserver (REQ, RES) {RES.WRITEHEAD (200); Res. “Hello World \ n”); console. LOG (‘Worker #’ + + ‘make a response’);}). Listen (8000);}

This code is very simple, the main thread is the currently running JS file, the main thread is based on the core of your local system Create a child process. All processes share a listener port 8000. When the request is initiated, the main thread will randomly assign the request to a child process.

Console.log (‘worker #’ + + ‘make a response’);
This code can print out which process processes the request.

As mentioned earlier, when the request is initiated, the system decides which process will be handed over to. This completely dependent on the system's load balancing is an important defect: on Windows, Linux and Solaris, as long as the ACCEPT Queue of a child is empty (usually the sub-process that is finally created), the system will put multiple Connetions. Allocated to the same child process, this will cause the process between the processes unbalanced. Especially when using long connections, the New COMING CONNECTION in the unit time is not high, and the Accept Queue of the child process is often empty, which will cause Connection to assign to the same process. So this load balance is completely dependent on Accept Queue's idleness, only in the case of short connection, and is very high, it can reach load balancing, but this time the system's LOAD will be very high, the system will become unstable stand up.

The above is the full content of this article, I hope this paper will bring a certain help to everyone’s learning or work, if you have any questions You can leave a message to communicate, thank you for your support of Tumi Cloud.

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment