Home>Article>Web Front-end> How nodejs handles intensive calculations
#How does nodejs handle intensive calculations?
Nodejs intensive CPU solution
First let’s talk about the advantages of nodejs single thread:
Recommended: "javascript Advanced Tutorial》
High performance, compared with PHP, it avoids the overhead of frequently creating and switching threads, executes faster, and takes up less resources.
Thread safety, no need to worry about the same variable being read and written by multiple threads, causing the program to crash.
Single-threaded asynchronous and non-blocking. In fact, the underlying nodejs access I/O is still multi-threaded. Blocking/non-blocking and asynchronous/synchronization are two different concepts. Synchronization does not mean blocking, but blocking definitely does. Synchronous; it’s a bit convoluted. Please give me an example. I went to the canteen to prepare a meal. I chose Set A, and then the staff helped me prepare the meal. If I just stood aside and waited for the staff to prepare the meal for me, this situation would be This is called synchronization; if the staff is helping me prepare food, the people behind me start ordering, so that the ordering service in the entire canteen does not stop because I am waiting for set meal A. This situation is called Non-blocking. This example simply illustrates the synchronous but non-blocking situation. Furthermore, if I buy a drink while waiting for the meal to be served, and then go back to get the set meal when my number is called, my drink has already been purchased. In this way, while waiting for the meal to be served, I also perform the task of buying a drink. Calling the number is equivalent to Once the callback is executed, it is asynchronous and non-blocking. If when I buy a drink, my number has been called for me to get the set meal, but I have to wait a long time to get the drink, so I may not get set meal A until a long time after my meal number is called in the lobby, which is Single thread blocking situation.
Multi-threading:
Thread is a basic unit of CPU scheduling. A CPU can only execute one thread task.
nodejs can also perform multi-thread tasks, such as referencing the TAGG/TAGG2 module, but both tagg/tagg2 use the pthread library and the V8::Isolate class to implement the js multi-threading function. According to the rules, we Functions executed in threads cannot use the core API of nodejs, such as fs and crypto modules, so there are still great limitations.
Multiple processes:
In browsers that support HTML5, we can use webworker to throw some time-consuming calculations into the worker process for execution, so that the main process will not be blocked and the user There will be no lag feeling.
Here we need to use the child_process module of nodejs. Child_process provides the fork method, which can start a nodejs file and regard it as a worker process. After the worker completes its work, the result will be sent to the main process, and then the worker will automatically Exit, so that we can use multiple processes to solve the problem of main thread blocking.
var express = require('express'); var fork = require('child_process').fork; var app = express(); app.get('/', function(req, res){ var worker = fork('./work.js') //创建一个工作进程 worker.on('message', function(m) {//接收工作进程计算结果 if('object' === typeof m && m.type === 'fibo'){ worker.kill();//发送杀死进程的信号 res.send(m.result.toString());//将结果返回客户端 } }); worker.send({type:'fibo',num:~~req.query.n || 1}); //发送给工作进程计算fibo的数量 }); app.listen(7878);
We listen to port 7878 through express. For each user request, we will fork a child process, pass the parameter n to the child process by calling the worker.send method, and at the same time listen to the message event of the message sent by the child process. , respond the result to the client.
The following is the content of the forked work.js file:
var fibo = function fibo (n) {//定义算法 return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1; } process.on('message', function(m) { //接收主进程发送过来的消息 if(typeof m === 'object' && m.type === 'fibo'){ var num = fibo(~~m.num); //计算jibo process.send({type: 'fibo',result:num}) //计算完毕返回结果 } }); process.on('SIGHUP', function() { process.exit();//收到kill信息,进程退出 });
We first define the function fibo to calculate the Fibonacci array, and then listen to the messages sent by the main thread. The calculation is completed Then send the result to the main thread. At the same time, it also listens to the SIGHUP event of the process. When this event is triggered, the process exits.
One thing we need to note here is that the kill method of the main thread does not actually cause the child process to exit, but triggers the SIGHUP event of the child process. The real exit still relies on process.exit().
Summary:
Using the fork method of the child_process module can indeed allow us to solve the blocking problem of single-threaded CPU-intensive tasks. At the same time, it is impossible to use Node.js without the tagg package. Limitations of the core API.
Single-threaded asynchronous Node.js does not mean that it will not block. Doing too many tasks on the main thread may cause the main thread to get stuck and affect the performance of the entire program, so we must be very careful when processing a large number of tasks. For CPU-intensive tasks such as looping, string splicing, and floating-point operations, various technologies are reasonably used to assign tasks to sub-threads or sub-processes to complete, keeping the Node.js main thread open.
The use of threads/processes is not without overhead. Reducing the number of threads/processes created and destroyed as much as possible can improve the overall performance and error probability of our system.
The above is the detailed content of How nodejs handles intensive calculations. For more information, please follow other related articles on the PHP Chinese website!