Home > Web Front-end > JS Tutorial > body text

Node single thread realizes high concurrency principle

小云云
Release: 2018-02-07 13:36:40
Original
2534 people have browsed it

This article mainly introduces the principle of node single-threaded high concurrency and node asynchronous I/O. It has certain reference value. If you are interested, you can learn about it. I hope it can help everyone.

1. Principle of node single-threaded implementation of high concurrency

As we all know, nodejs is a single-threaded scripting language that supports high concurrency. But why can single-threaded nodejs support high concurrency? Many people don’t understand its principle. Let me talk about my understanding below:

1. Advantages of node: I/O-intensive processing is the strength of node, because node’s I/O requests are all asynchronous (such as: sql query request, file stream operation request, http request...)

a. What is asynchronous?

Asynchronous: Issue operation instructions, and then you can do other things. After all operations are completed, the callback is executed.

Asynchronous implementation principle:


// 第一步:定义变量
let a = 1;

// 第二步:发出指令,然后把回调函数加入异步队列(回调函数并没有执行)
setTimeout(() => {
 console.log(a);
}, 0)
// 第三步:赋值,回调函数没有执行
a = 2;
// 第四步:发出指令,然后把回调函数加入异步队列(回调函数并没有执行)
setTimeout(() => {
 console.log(a);
}, 0)
// 第五步:赋值,回调函数没有执行
a = 3;
// 当所有代码执行完毕,cpu空闲下来了,就会开始执行异步队列里面的回调函数
// 所以最后控制台输出:3 3
Copy after login

b. What is asynchronous I/O?

As the name suggests, asynchronous I/O is to issue I/O requests asynchronously

c. Although nodejs can issue I/O requests asynchronously, nodejs does not support multi-threading. Why can it support it? What about high concurrency?

Because of the I/O operation of nodejs, multi-threading is enabled at the bottom layer

When there are multiple IO requests at the same time, the main thread will create multiple eio threads to improve the efficiency of IO requests. Processing speed

Additional knowledge points:

d. Although nodejs's I/O operations enable multi-threading, all threads are started based on the main thread and can only run in one process. Unable to fully utilize cpu resources

pm2 process manager can solve this problem

pm2 is a process manager for Node applications with load balancing function.

e. cpu The relationship between the number of cores and threads

In the past single-CPU era, a single task could only execute a single program at one point in time. Later, it developed into the multi-tasking stage, where the computer can execute multiple tasks or processes in parallel at the same point in time. Although it is not the "same point in time" in the true sense, multiple tasks or processes share a CPU, and it is left to the operating system to complete the running switching of the CPU between multiple tasks, so that each task has the opportunity to obtain certain benefits. The time slice runs. Now with multi-core CPUs, multiple tasks can be executed at the same time. The specific CPU core on which the task is run depends on the design of the operating system and the CPU itself.

2 . Disadvantages of node: Not good at CPU-intensive operations

a. What are CPU-intensive operations (complex operations, image operations)


// 这就是一个cpu密集型的操作
for (let i = 0; i < 1000000; i++) {
 console.log(i);
}
Copy after login

b. Why is nodejs not good at cpu-intensive operations

Because nodejs is single-threaded

Related recommendations:

Single-threaded JS Detailed explanation of execution issues

Detailed explanation of the characteristics of JS single-threaded asynchronous io callback

Detailed introduction to some things about JavaScript single-thread (picture)

The above is the detailed content of Node single thread realizes high concurrency principle. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template