Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the flow of the Node event loop

Shulou Source: shulou.com Published: 2022-06-02 22:31:16 10月03日 Update

This article mainly explains "what is the flow of Node event cycle". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "what is the flow of Node event cycle" together!

We all know that most of the applications we use today are event-driven. In the process of interacting with applications, many events will occur, such as clicks, double clicks, three clicks, etc., and many events will occur during the completion of file reading or certain tasks.

We all know that Node.js is a single-process, single-threaded, but can achieve high concurrency, which is inseparable from our event mechanism. Specifically, it is achieved through event loops, and high concurrency can be achieved through asynchronous callback interfaces and event drivers. [Recommended learning: nodejs tutorial]

event loop

When Node.js is running in a single thread, it is equivalent to entering a "dead" loop. The callback function on each asynchronous event is equivalent to checking your girlfriend on your mobile phone. If your girlfriend finds something "bad"(event), she will quarrel with you (call callback function_quarrel function). Until you don't see these things (events) in the future, you will no longer look at your mobile phone (exit the endless loop).

The event loop also typically hands off the corresponding operation to the operating system, which we all know is basically multithreaded. That is to say, we know that this girlfriend named Node.js usually doesn't just look at our phone, she also hires a professional monitoring team to monitor your phone. In this way, you can monitor the team to check your mobile phone information (operating system background processing), when processing part of the information immediately notify your girlfriend (a certain operation is completed, immediately notify Node.js), and then girlfriend will be based on the nature of the information to do things into the queue (Node.js will add the relevant callback function to the polling queue), and then one by one execution.

Here is a simplified diagram of the event cycle for Node.js:

In the above figure, each stage will have a FIFO queue of executable callback functions. Usually, when the event loop enters one of the stages, the operation of that stage will be executed, and then the callbacks in the queue of that stage will be executed until the queue ends or the limit of the number of callbacks is reached. After that, the event loop will enter the next stage, and so on.

From this diagram, we can see that the sequence of events is roughly:

incoming data-> poll-> check-> close callback-> timers-> pending callbacks-> idle, prepare-> poll

Thank you for reading, the above is the content of "What is the flow of Node event cycle", after learning this article, I believe that everyone has a deeper understanding of what the flow of Node event cycle is, and the specific use situation needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Events loops functions girlfriends mobile phones queues stages processes learning operating systems information systems threads processing monitoring things content teams applications situations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Tech Info Apple Microsoft vpn