node. JS how to make full use of multi-core CPU

  • summary
  • node. JS method of making full use of multi-core CPU
  • Four ways for node to create child processes


Nodejs is built based on the V8 engine of Chrome browser, which shows that its model is similar to the browser. Our JavaScript will run on a single thread of a single process.

However, the single process and single thread of V8 engine is not a perfect structure. Nowadays, CPUs are basically multi-core. Real servers often have several CPUs (for example, our online physical machine has 12 cores), so this will throw a problem in the practical application of nodejs: “how to make full use of multi-core CPU servers?”

Strictly speaking, node is not really a single threaded architecture, because node itself has I / O threads (network I / O and disk I / O). These I / O threads are processed by libuv at the lower level, which is transparent to JavaScript developers. Javascript code always runs on V8 and is single threaded. So on the surface, nodejs is single threaded.

node. JS method of making full use of multi-core CPU

Facing the problem of low utilization of multi-core by single process and single thread, according to previous experience, each process can use one CPU, so as to realize the utilization of multi-core CPU. Node provides child_ Process module, and also provides fork () method to realize process replication (as long as process replication requires certain resources and time. Node replication process requires no less than 10m memory and no less than 30ms time).

Such a solution is the most classic master worker mode on * Nix system, also known as master-slave mode.

The distributed architecture of this typical parallel processing business model has good scalability (scalability is actually discussed together with parallel algorithms and parallel computer architecture. The scalability of an algorithm on a machine reflects whether the algorithm can effectively use the increasing CPU.) And stability.

The main process is not responsible for specific business processing, but for scheduling and managing work processes. Work processes are responsible for specific business processing. Therefore, developers need to pay attention to the stability of work processes.

The process copied through fork () is an independent process with independent and brand-new V8 instances. Although node provides fork () to copy the process so that each CPU core can use it, we should still remember that the fork () process is very expensive. Fortunately, node can handle large concurrent requests on a single thread through event driven.

Note: starting multiple processes here is only to make full use of CPU resources, not to solve the concurrency problem.

Four ways for node to create child processes


Create a child process to execute commands


Create a child process to execute commands. Unlike spawn (), the method parameters are different. It can pass in a callback function to get the status of the child process


Start a subprocess to execute the specified file. Note that the shebang symbol (#!) must be declared at the top of the file Used to specify the process type.


Similar to spawn (), the difference is that it only needs to execute the JavaScript file module to create the child process of node.

Note: the following three methods are all extended applications of spawn().

The above is node JS how to make full use of multi-core CPU details, more about node JS make full use of multi-core CPU information, please pay attention to other related articles of developeppaer!

Recommended Today

JS generate guid method

JS generate guid method Globally unique identification(GUID) is an algorithm generatedBinaryCount Reg128 bitsNumber ofidentifier , GUID is mainly used in networks or systems with multiple nodes and computers. Ideally, any computational geometry computer cluster will not generate two identical guids, and the total number of guids is2^128In theory, it is difficult to make two […]