OverviewNodejs is built based on the V8 engine of the Chrome browser, which means that its model is similar to that of the browser. Our JavaScript will run on a single thread in a single process. However, the single-process and single-thread structure of the V8 engine is not a perfect structure. Nowadays, CPUs are basically multi-core. Real servers often have several CPUs (like our online physical machine has 12 cores), so this will raise a question in the actual application of Nodejs: "How to make full use of multi-core CPU servers?" Strictly speaking, Node is not a true single-threaded architecture, because Node itself has I/O threads (network I/O, disk I/O). These I/O threads are handled by the lower-level libuv, which is transparent to JavaScript developers. JavaScript code always runs on V8 and is single-threaded. So on the surface NodeJS is single-threaded. How to make full use of multi-core CPU in node.jsFaced with the problem of low multi-core utilization of single-process and single-thread, according to previous experience, each process can use one CPU to realize the utilization of multi-core CPUs. Node provides the child_process module and also provides the fork() method to implement process replication (as long as the process is replicated, certain resources and time are required. Node requires no less than 10M of memory and no less than 30ms to replicate the process). Such a solution is the most classic Master-Worker mode on *nix systems, also known as master-slave mode. This distributed architecture of a 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 certain machine reflects whether the algorithm can effectively utilize the increasing number of CPUs.) and stability. The main process is not responsible for specific business processing, but is responsible for scheduling and managing the working process. The working process is responsible for specific business processing, so the stability of the working process is what developers need to pay attention to. The process copied by fork() is an independent process, which has an independent and brand new V8 instance. Although Node provides fork() to copy the process so that each CPU core can be used, it is still important to 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 just to make full use of CPU resources, not to solve concurrency problems. 4 ways to create child processes in Node1. spawn() Create a subprocess to execute the command 2. exec() Create a child process to execute the command. The difference from spawn() is that the method parameters are different. It can pass in a callback function to get the status of the child process. 3. execFile() Starts a subprocess to execute the specified file. Note that the SHEBANG symbol (#!) must be declared at the top of the file to specify the process type. 4. fork() Similar to spawn(), the difference is that it only needs to execute the JavaScript file module to create a Node child process. Note: The following three methods are extended applications of spawn(). The above is the details of how node.js can make full use of multi-core CPUs. For more information about how node.js can make full use of multi-core CPUs, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: How to quickly build a LAMP environment on CentOS platform
>>: Detailed explanation of Apache website service configuration based on Linux
1. Install MySQL database ① Download and unzip an...
1. Install Zabbix Agent to monitor the local mach...
Table of contents Introduction Child Process Crea...
Download the zip installation package: Download a...
A. Installation of MySQL backup tool xtrabackup 1...
When we write CSS, we sometimes forget about the ...
This article shares the specific code of jQuery t...
In Black Duck's 2017 open source survey, 77% ...
Table of contents 1. Global Guard 1.1 Global fron...
Preface During the development process, you will ...
Table of contents Vue2 Writing Vue3 plugin versio...
First, let's take a look at my basic developm...
The final result is like this, isn’t it cute… PS:...
How to write transparent CSS for images using filt...
Table of contents Causes of MySQL Table Fragmenta...