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

Detailed explanation of blocking and non-blocking in Nodejs

青灯夜游
Release: 2021-07-01 09:51:44
forward
2099 people have browsed it

This article will help you understand blocking and non-blocking in Nodejs. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Detailed explanation of blocking and non-blocking in Nodejs

Recommended study: "nodejs Tutorial"

Everyone should have some ideas about the two concepts of blocking and non-blocking Understanding:

To put it simply, blocking is mostly caused by synchronous mode. Non-blocking can be understood as asynchronous mode processing some time-consuming operations.

So what are blocking and non-blocking in Node.js? How is blocking described? The following mainly explains this issue:

What is blocking in Node?

I/O operations can be understood as mainly referring to interaction with the system disk (data reading and writing) or network requests, etc.

Blocking is the execution of other JS codes You must wait until the previous time-consuming I/O operations or some network requests are completed. Because Node has an event loop to solve this problem, if the event loop is not turned on during the execution of js, it will actually cause blocking.

In fact, in Node.js, there is also a situation of being blocked under normal circumstances. The reason is that Node's asynchronous processing is more friendly to I/O operations [using event loop], but for some CPUs that may exist in JavaScript Intensive operation performance is relatively low. Some students may ask, why can’t Node.js support CPU-intensive operations asynchronously? Because it can be understood that these CPU-intensive operations are actually some synchronous codes, such as a large number of for loops, massive data calculations, etc.

There are also some synchronization methods in the standard library of Node.js, most of which are based on libuv to achieve blocking effects. There are also some blocking methods in Node's native modules. However, Node will also provide a corresponding asynchronous version of the API.

What is libuv doing? It is a library for asynchronous I/O operations that supports multiple platforms. For details, please see the official website: https://libuv.org/

What is non-blocking in Node?

First think about how to achieve non-blocking (asynchronous) in a context that is not limited to Node?

Simply two:

1) Multiple threads can be opened to handle concurrent operations

2) Event loop mode, if there are asynchronous operations placed in the event queue , after the asynchronous operation ends, call the corresponding callback function to process the asynchronous return result

Node.js is single-threaded. The reason is: the outer layer of Node.js is implemented by JavaScript, and the interpretation and execution of JavaScript is through V8 engine is made.

Since the execution of JS is single-threaded, it is impossible for us to open another thread for interpretation and execution during JS interpretation and execution. Therefore, Node.js uses the second method to achieve non-blocking (asynchronous operation) )of.

Be careful not to mix blocking and non-blocking APIs in Node

English original address: https://nodejs.org/en/docs/guides/ blocking-vs-non-blocking/

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of Detailed explanation of blocking and non-blocking in Nodejs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!