How does node.js 'multi-threading' handle high-concurrency tasks?
The following article will introduce to you how to use nodejs "multi-threading" to handle high-concurrency tasks. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Related recommendations: "nodejs video tutorial"
Moore's Law
Moore The law was proposed by Intel co-founder Gordon Moore in 1965, that is, the number of components that can be accommodated on an integrated circuit will double every 18 to 24 months, and the performance will also increase by one times. That is, processor (CPU) performance doubles every approximately two years.
More than 50 years have passed since Moore’s Law was proposed. Today, as chip components get closer to the scale of a single atom, it becomes increasingly difficult to keep up with Moore's Law.
In 2019, NVIDIA CEO Jen-Hsun Huang said at the ECS exhibition: "Moore's Law used to grow 10 times every 5 years and 100 times every 10 years. But today, Moore's Law can only grow by a few percentage points every year. , maybe only 2 times every 10 years. Therefore, Moore's Law is over."
The performance of a single processor (CPU) is getting closer and closer to the bottleneck. If you want to break through this bottleneck, you need to make full use of Multi-threading technology
allows a single or multiple CPU
to execute multiple threads at the same time to complete computer tasks faster.
Node's multi-threading
We all know that Javascript
is a single-threaded language, Nodejs
uses Javascript
features, using the event-driven model, implement asynchronous I/O, and behind asynchronous I/O is multi-thread scheduling.
Node
For the implementation of asynchronous I/O, you can refer to Pu Ling's "In-depth introduction to Node.js"
In the Go
language, you can create Goroutine
to explicitly call a new thread, and control the maximum number of concurrencies through the environment variable GOMAXPROCS
.
In Node
, there is no API
that can explicitly create a new thread. Node
implements some asynchronous I/O APIs, such as fs.readFile
, http.request
. The bottom layer of these asynchronous I/O is to call new threads to perform asynchronous tasks, and then use the event-driven model to obtain the execution results.
Server-side development and tool development may require the use of multi-threaded development. For example, use multi-threads to handle complex crawler tasks, use multi-threads to handle concurrent requests, use multi-threads for file processing, etc...
When we use multi-threads, we must control the maximum number of simultaneous concurrencies. Because the maximum number of concurrencies is not controlled, errors caused by file descriptor
exhaustion, network errors caused by insufficient bandwidth, errors caused by port restrictions, etc. may occur.
There is no API
or environment variable for controlling the maximum number of concurrencies in Node
, so next, we will use a few simple lines of code to implement it.
Code Implementation
Let’s first assume the following demand scenario. I have a crawler that needs to crawl 100 Nugget articles every day. If one article Crawling is too slow. Crawling 100 articles at a time will cause many requests to fail directly due to too many network connections.
Then we can implement it, request 10 articles each time, and complete it in 10 times. This can not only increase efficiency by 10 times, but also ensure stable operation.
Let’s take a look at a single request task. The code is implemented as follows:
const axios = require("axios"); async function singleRequest(article_id) { // 这里我们直接使用 axios 库进行请求 const reply = await axios.post( "https://api.juejin.cn/content_api/v1/article/detail", { article_id, } ); return reply.data; }
For the convenience of demonstration, here we request the same address 100 times. Let’s create 100 request tasks. The code The implementation is as follows:
// 请求任务列表 const requestFnList = new Array(100) .fill("6909002738705629198") .map((id) => () => singleRequest(id));
Next, let’s implement the concurrent request method. This method supports executing multiple asynchronous tasks at the same time and can limit the maximum number of concurrencies. After a task in the task pool is executed, a new asynchronous task will be pushed to continue execution to ensure high utilization of the task pool. The code is implemented as follows:
const chalk = require("chalk"); const { log } = require("console"); /** * 执行多个异步任务 * @param {*} fnList 任务列表 * @param {*} max 最大并发数限制 * @param {*} taskName 任务名称 */ async function concurrentRun(fnList = [], max = 5, taskName = "未命名") { if (!fnList.length) return; log(chalk.blue(`开始执行多个异步任务,最大并发数: ${max}`)); const replyList = []; // 收集任务执行结果 const count = fnList.length; // 总任务数量 const startTime = new Date().getTime(); // 记录任务执行开始时间 let current = 0; // 任务执行程序 const schedule = async (index) => { return new Promise(async (resolve) => { const fn = fnList[index]; if (!fn) return resolve(); // 执行当前异步任务 const reply = await fn(); replyList[index] = reply; log(`${taskName} 事务进度 ${((++current / count) * 100).toFixed(2)}% `); // 执行完当前任务后,继续执行任务池的剩余任务 await schedule(index + max); resolve(); }); }; // 任务池执行程序 const scheduleList = new Array(max) .fill(0) .map((_, index) => schedule(index)); // 使用 Promise.all 批量执行 const r = await Promise.all(scheduleList); const cost = (new Date().getTime() - startTime) / 1000; log(chalk.green(`执行完成,最大并发数: ${max},耗时:${cost}s`)); return replyList; }
As can be seen from the above code, the key to using Node
for concurrent requests is Promise.all
, Promise.all
Multiple asynchronous tasks can be executed simultaneously.
In the above code, an array with a length of max
is created, and the corresponding number of asynchronous tasks is placed in the array. Then use Promise.all
to execute these asynchronous tasks at the same time. When a single asynchronous task is completed, a new asynchronous task will be taken out of the task pool to continue execution, maximizing efficiency.
Next, we use the following code to perform the execution test (the code is implemented as follows)
(async () => { const requestFnList = new Array(100) .fill("6909002738705629198") .map((id) => () => singleRequest(id)); const reply = await concurrentRun(requestFnList, 10, "请求掘金文章"); })();
The final execution result is as shown below:
At this point, our concurrent request is completed! Next, let’s test the speed of different concurrencies respectively~ The first is 1 concurrency, that is, no concurrency (as shown below)
It takes 11.462 seconds! When concurrency is not used, the task takes a very long time. Next, let’s take a look at how long it takes under other concurrency conditions (as shown below)
As you can see from the above figure, as our number of concurrency increases, the task execution speed becomes faster and faster! This is the advantage of high concurrency, which can improve efficiency several times or even dozens of times in some cases!
If we take a closer look at the above time-consuming, we will find that as the number of concurrency increases, the time-consuming will still have a threshold and cannot completely increase in multiples. This is because Node
actually does not open a thread for each task for processing, but only opens a new thread for asynchronous I/O
tasks. Therefore, Node
is more suitable for processing I/O
intensive tasks, but not suitable for CPU
(computing) intensive tasks.
At this point, we have finished introducing the use of Node "multi-threading" to handle high-concurrency tasks. If you want the program to be more perfect, you also need to consider the task timeout and fault tolerance mechanism. If you are interested, you can implement it yourself.
For more programming-related knowledge, please visit: Introduction to Programming! !
The above is the detailed content of How does node.js 'multi-threading' handle high-concurrency tasks?. For more information, please follow other related articles on the PHP Chinese website!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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
