#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!

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools