Original text: nodejs.org/en/docs/gui…

You should have some ideas about blocking and non-blocking:

Briefly, most of the blocking is due to synchronous mode, while non-blocking can be interpreted as asynchronous mode processing some time-consuming operations

How are blocking and non-blocking described in Node.js? The following is mainly to expand the explanation of this problem:

What is blocking in Node?

I/O operations can be understood as mainly interactions with system disks (data reads and writes) or network requests

Blocking is when the execution of other JS code must wait until the previous time-consuming I/O operation or some network request has completed. Since Node has an event loop to solve this problem, if the event loop is not enabled during js execution, it will cause a blocking situation.

Node.js is normally blocked because Node’s asynchronous processing is friendly for I/O operations (using event loops), but the performance of some of the CPU intensive operations that JavaScript might have is low. Some students might say, why can’t CPU intensive node.js operations be supported asynchronously? It is understandable that these CPU-intensive operations are actually synchronous code, such as a lot of for loops, a lot of data computation, etc.

The Node.js standard library also has some synchronized methods, most of which are based on Libuv to implement blocking effects. There are also blocking methods in Node’s native modules. Node also provides an asynchronous version of the API.

What’s libuv? Is a multiplatform library for asynchronous I/O operations. Details can be found on the official website: libuv.org/

What is non-blocking in Node?

Let’s start by thinking about how to implement non-blocking (asynchronous) in a context beyond Node.

Just two things:

1) Multiple threads can be opened to handle concurrent operations

2) The mode of event loop, if there is an asynchronous operation in the event queue, after the asynchronous operation, call the corresponding callback function to process the asynchronous return result

Node.js is single-threaded for one reason: The outer layer of Node.js is implemented by JavaScript, whose interpretation execution is done by the V8 engine.

Since JS execution is single threaded, it is not possible to open another thread during JS interpretation execution, so Node.js takes a second approach to non-blocking (asynchronous operation).

Be careful not to mix blocking and non-blocking apis in nodes