[node. js] Cluster, use multiple cores.

Source: Internet
Author: User

Introduction

As we all know, although the bottom of node has an IO thread pool, its application layer is single-threaded, and is a waste of resources for multi-core CPU environments.

Fortunately, node provides a child_process module that allows developers to open multiple processes, each using a single CPU for multi-core utilization.

The Child_process module gives node the ability to create child processes at will. Because the Child_process class itself is a eventemitter, interprocess communication is easy, and parent-child interprocess communication does not pass through the network layer, but is done in the kernel, efficiently.

But child_process for developers, the programming model is too complex to worry about too much detail, such as: The parent process crashes, sub-process recycling is required by the developer to provide code to deal with-if the developers just want to take advantage of the multi-core model, the control granularity of the specific work process is not too many assumptions , the development model is undoubtedly a headache.

For this issue, Node provides the Cluster module. Cluster simplifies the parent-child model programming model, only distinguishes between: the current process is not master, is master can fork the child process, not then please exercise worker duties. As for what resources are recycled, the allocation of load, the handling of uncaughtexception, it has its own arrangement.

Essentially, Cluster is a combined application of child_process and net modules. Not only does it simplify the programming model, it also makes it possible for shared listening to be the same port.

More about the principle of cluster here is not the table, interested can shift → interpretation nodejs multi-core processing module cluster

Scene

With node Fast station, of course, with express so, demand is to use Cluster to do a single cluster, multi-process run express, improve the site's throughput.

Experiment

First use Express-generator to set a frame, template engine or make Ejs bar:

Express-e MYAPP2

Express-generator by default will generate a good app.js, a line of code is not written, a complete HTTP server is already implemented. So, we can implement a Cluster stand-alone cluster, Clustermaster.js:

//CPU several cores? varCPUs = require (' OS '). CPUs (). length; //child process Listener Message handler functionvarWorkerlistener =function(msg) {if(msg.access) Console.log (' User access%s, worker [%d] ', msg.access, Msg.workerid);};//Fork New sub-process functionvarForkworker =function(listener) {varWorker =cluster.fork (); Console.log (' Worker [%d] has been created ', Worker.process.pid); Worker.on (' Message ', listener); returnworker;}; //Cluster processingvarCluster = require (' cluster '));if(cluster.ismaster) { for(vari = 0; I < CPUs; i++) {forkworker (Workerlistener); }} Else {    varApp = require ('./app ')); returnApp.listen (3000);} //cluster receive the child process exit messageCluster.on (' exit ',function(worker, Code, signal) {Console.log (' Worker [%d] died%s, fork a new one. ', Worker.process.pid, code||signal); Forkworker (Workerlistener);});//cluster receive child process run messageCluster.on (' online ',function(worker) {Console.log (' Worker [%d] is running. ', worker.process.pid);});

can see:

    • Cluster is a ismaster to differentiate parent-child processes. Process creation logic in the parent process: Create a corresponding number of child processes based on the number of cores in the CPU, and run the specific Server creation code in the child process. Simple enough model ~
    • On the Cluster parent process side, the current worker is always available. The current worker PID, in the case of a worker handle, corresponds to woker.process.pid; In the context in which the child process is running, it is process.pid. It's not hard to understand ~
    • We've added a message handler for the new fork, which allows any child process to run code that can be triggered at any time by using a message: It's a very broad-based, and we'll show you an example later.
    • When the child process exits, the Cluster parent process receives the exit message, which then re-fork a new child process to fill in

Suppose the user visits this site: localhost:3000/, I want to tell him which child process is rendering the page for him. Then, add an express route index.js in/routes/:

var express = require (' Express '); var router =/**/router.get (function  (req, res) {    res.render (' index ', {title: ' Express ',                               workerid:process.pid});    ' /'= router;

As you can see, in addition to routing, you also send a message using process.send, which actually triggers the workerlistener of the previous code's sub-process registration, reports the path and process number to it, and because of the use of render, it is naturally the Ejs template,/views/ Index.js:

<!DOCTYPE HTML><HTML>  <Head>    <title><%=title%></title>    <Linkrel= ' stylesheet 'href= '/stylesheets/style.css '/>  </Head>  <Body>    <H1><%=title%></H1>    <P>Welcome to<%=title%></P>    <P>[<%=Workerid%>] worker is serving.</P>  </Body></HTML>

The template renders the child process PID that is carried by render to the page, and, of course, adds a route map to the App.js:

var routes = require ('./routes/index '); App.use ('/', routes);

Run the program and look at the process generation situation under the shell:

A father and four sons, try to kill a child process with kill 64163. As you can see from the console, the main process receives the exit message and then re-fork a new process:

Test the load and repeat the site several times with Chrome, as if the worker child process has remained the same:

Use AB to simulate concurrency and make it a core that is not busy:


Change Safari Browser to visit the site, this time for a worker process to serve, the speed is not slow

More articles please visit my blog new address: http://www.moye.me/

[node. js] Cluster, use multiple cores.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.