What is async?Asynchronous and synchronous should be a topic that is often discussed. The concept of synchronization is very simple. It is executed from top to bottom, and the lower part will not be executed until the upper part is executed. Asynchronous processing allows you to submit a command first, execute other transactions in the middle, and then return to the previous task after the execution is completed. For example: You are lucky to have a beautiful girlfriend. One day your girlfriend texts you and asks you what movie you want to watch in the evening? But you don't know what to watch, so you immediately turn on your computer and check the latest popular movies, in which your girlfriend has been waiting for you. This is synchronization. And what about asynchronously? Or your girlfriend texts you asking you what movie you watched, and you tell her: Please wait a moment, let me check it out, and then you call her back to tell her. That's asynchrony. From this we can see some characteristics of synchronization and asynchrony: 1. It must happen to two objects. (You and your girlfriend) 2. Something has to happen. (See a movie) The difference is that synchronous execution is sequential execution, and one is executed after another until the execution is completed, while asynchronous execution is to execute one first, and then execute another one before the execution is completed, and return the result after the first execution is completed. Why do we need asynchrony?The answer is obvious. In order to improve work efficiency, the CPU computing speed and the disk reading and writing speed are too far apart, and disks are in short supply. Therefore, the computer storage system has a layered design that balances efficiency and cost. It can be said that laziness drives human progress. Any method that can reduce the time spent and achieve the same effect will definitely be given priority. The time spent waiting for a reply from the other party when sending a text message is purely wasted, and the waiting time for the CPU to write to the disk and wait for the result to be returned is also ruthlessly consumed. This is completely intolerable in an era that emphasizes efficiency. Therefore, bosses pursue the goal of keeping employees busy all the time and maximizing their value. It is also a requirement for efficiency to keep the CPU and disk processing transactions at full capacity. Hence, asynchronous processing came into being. What is asynchronous IO?Asynchronous IO refers to the IO (data input and output) capability provided by the operating system. For example, keyboard input corresponds to a dedicated data output interface on the display. This is the IO capability we can see in our lives. This interface will go down to the operating system level. In the operating system, it will provide many capabilities, such as: disk reading and writing, DNS query, database connection, network request processing, etc. The performance is inconsistent at different operating system levels. Some are asynchronous and non-blocking; some are synchronous and blocking. In any case, we can regard them as data interaction between the upper layer and the lower layer systems. The upper layer depends on the lower layer, but in turn, the upper layer can also transform the capabilities provided by the lower layer. If the operation is asynchronous and non-blocking, then this is an asynchronous and non-blocking asynchronous IO model; if it is synchronous and blocking, then it is a synchronous IO model. Koa is an upper-level web service framework, all implemented by js. It has interactions between operating systems, all implemented through nodejs; for example, nodejs's readFile is an asynchronous non-blocking interface, and readFileSync is a synchronous blocking interface. What is an event loop?The event loop refers to the non-blocking I/O operations performed by Node.js. Although JavaScript is single-threaded, since most kernels are multi-threaded, Node.js will load the operations to the system kernel as much as possible. Therefore they can handle multiple operations being performed in the background. When one of these operations completes, the kernel tells Node.js so that Node.js can add the corresponding callback to the polling queue for eventual execution. In other words, js is single-threaded, but node is actually multi-threaded when running. (Personal understanding) The message queue is a first-in-first-out queue, which stores various messages. V8 EngineThe Chrome engine and nodejs engine we often talk about are the V8 engine, which is roughly composed of the following: This engine consists of a memory heap and a call stack. The memory heap is responsible for memory allocation, such as variable assignment, and the call stack is where the code is executed. The main thread code is executed sequentially in the call stack. When the call stack is empty, the js engine will go to the message queue to get the message. Execute it after getting it. JavaScript is a single-threaded programming language, meaning it has a single call stack. Therefore it can only do one thing at a time. The call stack is a data structure that basically keeps track of where we are in our program. If we step into a function, we put this data on the top of the stack. If we return from a function, these data will be popped off the top of the stack. That's what the stack is for. Each entry in the call stack is called a stack frame. The difference between a heap and a stack is that one is first in, first out, and the other is last in, first out. When js runsSome APIs we often use are not provided by the js engine, such as the timer setTimeout. They are actually provided in the browser, that is, at runtime, so there are actually other components besides the JavaScript engine. One of the components is provided by the browser, called Web APIs, such as DOM, AJAX, setTimeout and so on. Then there are the very popular event loops and callback queues. The runtime is responsible for sending messages to the engine thread. It is only responsible for producing messages, not for retrieving messages. Message QueuesWhen the main thread encounters an asynchronous task during execution, it initiates a function or registers a function, and notifies the corresponding worker thread (such as ajax, dom, setTimout, etc.) through the event loop thread. At the same time, the main thread continues to execute backward without waiting. When the worker thread completes the task, the eventloop thread will add the message to the message queue. If the call stack on the main thread is empty at this time, the message at the front of the message queue will be executed in sequence. When a new message enters the queue, it is automatically placed at the end of the queue. Single-threaded means that js tasks need to be queued. If the previous task has a large number of time-consuming operations, the subsequent tasks cannot be executed, and the accumulation of tasks will cause the page to "pseudo-death". This is also the "pitfall" that js programming has always emphasized to avoid. The main thread will loop the above steps. The event loop is the process in which the main thread repeatedly takes messages from the message queue and executes them. It should be noted that the GUI rendering thread and the JS engine are mutually exclusive. When the JS engine is executing, the GUI thread will be suspended, and the GUI updates will be saved in a queue and executed immediately when the JS engine is idle. Therefore, page rendering is performed when the main thread call stack of the js engine is empty. In fact, the maintenance of the event loop mechanism and the message queue is controlled by the event-triggered thread. The event triggering thread is also provided by the browser rendering engine, which maintains a message queue. When the JS engine thread encounters asynchrony (DOM event monitoring, network request, setTimeout timer, etc.), it will be handed over to the corresponding thread to maintain the asynchronous task alone, waiting for a certain time (timer ends, network request succeeds, user clicks DOM), and then the event triggers the thread to add the callback function corresponding to the asynchronous to the message queue, and the callback function in the message queue waits to be executed. At the same time, the JS engine thread will maintain an execution stack, and the synchronous code will be added to the execution stack in sequence and then executed, and will exit the execution stack when it ends. If the tasks in the execution stack are completed, that is, when the execution stack is empty (that is, the JS engine thread is idle), the event triggering thread will take out a task (that is, an asynchronous callback function) from the message queue and put it into the execution stack for execution. The above is the detailed analysis of the node event loop and message queue. For more information about the node event loop and message queue, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Database SQL statement optimization
>>: MySQL semi-synchronous replication principle configuration and introduction detailed explanation
Because using group by in MySQL always results in...
Preface Readers who are familiar with MySQL may f...
Table of contents 1. Union Type 2. Crossover Type...
Later, I also added how to use Jupyter Notebook i...
Table of contents introduction 1. MySQL master-sl...
1. First, prepare VS2019 and MySQL database. Both...
mysql basic data types Overview of common MySQL d...
You can easily input Chinese and get Chinese outp...
10.4.1 The difference between Frameset and Frame ...
background Today, while cooperating with other pr...
my.cnf is the configuration file loaded when MySQ...
MouseEvent When the mouse performs a certain oper...
We better start paying attention, because HTML Po...
What is a covering index? Creating an index that ...
uniapp code <template> <view> <ima...