4-Asynchronous: non-blocking IO

4-Asynchronous: non-blocking I_O

Introduction

What is non-blocking I/O in Node.js

  • I/O stands for Input/Output. It is the input and output of a system
  • The difference between blocking I/O and non-blocking I/O lies in whether the system can receive other input during the period from input to output.

To understand these two sentences , the system can be compared to a waiter. A blocking I/O waiter can only completely process one person's business at a time. After processing one person, he will go to the next person; The blocking I/O type waiter can first let the customer make a reservation, and after the reservation is completed, he can have the energy to handle another person's reservation. If a customer's reservation is completed, he can just notify the customer.
It seems that the operation mode of non-blocking I/O waiters is more in line with the current large number of customer groups, while the opposite is true for blocking I/O.

A simpler understanding , for example, if you encounter a difficult problem and ask a question online, and while you are waiting for the answer after asking the question, if you cannot carry out other work until the question is answered, then this is blocking I/O. , if you can do other work first, then it is non-blocking I/O.

Note : In the example of waiters and customers, there is a problem. If the number of waiters is small and there are many customers, the waiters cannot handle it, which will also cause business congestion. Therefore, when looking at blocking and non-blocking issues, we should look at it from a macro perspective and pay attention to The point is not to process the business, but to focus on the process of accepting business - waiting for business processing - business processing completion. In this process, while waiting for business processing, whether the waiter can handle other business is to judge whether blocking or non-blocking Important reference.

Understanding NodeJs through threading

Back to the NodeJs system diagram mentioned in NodeJs built-in modules.

After understanding the concepts of non-blocking and blocking I/O above, we can divide this picture into areas according to the thread mode.

As shown in the figure below, the left part is the node.js thread, and the right part is other C++ threads. When our application code on the left is working, it is running in the node.js thread. If the I/O operation in node.js is used (node (All I/O operations in .js are non-blocking), you need to put the operation in other c++ threads on the right for execution. After execution, the results are returned to the node.js thread on the left for use.

Therefore, according to the thread mode, it can be divided into node.js threads and c++ threads.

Moreover, the non-blocking implementation at the bottom of node.js is also implemented through threads.
image.png
Note : Asynchronous and non-blocking I/O are not the same thing. Asynchronous refers to a programming method, and non-blocking I/O is the name of an underlying mechanism.

Guess you like

Origin blog.csdn.net/qq_44886882/article/details/128776453