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
echarts word cloud is an extension of echarts htt...
1. Introduction to nmon Nmon (Nigel's Monitor...
1. Enter the following address in the browser htt...
An interesting discovery: There is a table with a...
1. Introduction This article does not have screen...
This article shares the specific code of javascri...
Search online to delete duplicate data and keep t...
Table of contents Preface 1. Parent component pas...
When Mysql occupies too much CPU, where should we...
Before the arrow was shot, the bow whispered to t...
Mysql 8.0 installation problems and password rese...
1 MySQL autocommit settings MySQL automatically c...
1. Query process show processlist 2. Query the co...
Table of contents Listener watch Format Set up th...
HTML implements 2-column layout, with fixed width...