Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the stages of the nodejs event loop

Shulou Source: shulou.com Published: 2022-06-02 15:43:08 10月03日 Update

This article introduces the relevant knowledge of "how many stages are there in the nodejs event cycle". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The stage of nodejs event cycle: 1, timers (timer) stage; 2, pending callbacks stage; 3, idle, prepare stage; 4, poll (polling) stage; 5, check (check) stage; 6, close callbacks stage.

This tutorial operating environment: windows7 system, nodejs version 12.19.0, DELL G3 computer.

Nodejs event loop

When Node.js starts, the event loop is initialized, and each event loop contains six loop phases, and the nodejs time loop is completely different from the browser event.

Note: each box in the figure is called a phase of the practice cycle, and the six phases are a cycle of events.

Stage generalization

Timers (timer): this phase executes the callback functions scheduled by setTimeout () and setInterval ().

Pending callbacks (I setImmediate O callback): almost all callback functions are executed at this stage, except for close callbacks (close callback) and those dispatched by timers and setImmediate ().

SetImmediate is approximately equal to setTimeout (callback,0)

Idle (idling), prepare: this phase is used internally only.

Poll (polling): retrieves the new Node O event; it blocks at this stage at the appropriate time.

Check (check): the callback of the setImmediate () setting is called at this stage.

Close callbacks (callback of the shutdown event): such as socket.on ('close',...) Callbacks of this type are called at this stage.

Between each run of the event loop, Node.js checks to see if it is waiting for an asynchronous Icano or timer, and automatically shuts down if it doesn't.

If event loop enters the poll phase and the code does not set timer, the following occurs:

If the poll queue is not empty, event loop will synchronously execute the callback in the queue until the queue is empty or the executed callback reaches the upper limit of the system

If poll queue is empty, the following occurs:

-if the code has been set by setImmediate (), callback,event loop enters the resulting poll phase into the check phase, and executes the queue of the check phase (the queue of the check phase is set by setImmediate).

-if the code does not set setImmediate (callback), event loop will block waiting for callback to join poll queue at this stage and execute as soon as it arrives.

If event loop enters the poll phase and the code sets timer:

If the poll queue is empty (since the poll phase is idle), event loop will check the timers, and if one or more timers times have arrived, the event loop will enter the timer phase in cyclic order and execute the timer queue.

This is the end of the content of "what are the stages of the nodejs event cycle". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Phase loop event code check timer situation time content function more state knowledge system scheduling blocking practical appropriate learned next Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Linux Apple Docker Huawei