Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the stages of the nodejs event loop

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report