Home > Web Front-end > Front-end Q&A > What are the stages of the nodejs event loop?

What are the stages of the nodejs event loop?

青灯夜游
Release: 2021-11-12 14:45:59
Original
2372 people have browsed it

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

What are the stages of the nodejs event loop?

The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, DELL G3 computer.

nodejs event loop

When Node.js starts, the event loop will be initialized. Each event loop will contain six cycle stages, nodejs time loop and browser The events were completely different.

What are the stages of the nodejs event loop?
Note: Each box in the figure is called a phase of the practice cycle, and these six phases are an event cycle.

Stage summary

  • ##timers (timers): This stage executes those specified by setTimeout () and setInterval() dispatched callback functions.
  • pending callbacks (I/O callbacks): This stage will execute almost all callback functions, except close callbacks(close callbacks) and those specified by timers and setImmediate() scheduled callbacks.
    setImmediate is approximately equal to setTimeout(callback, 0)
  • idle (idle), prepare: This stage is only used internally.
  • poll (polling): Retrieve new I/O events; Node will block at this stage when appropriate.
  • check (check): The callback set by setImmediate() will be called at this stage.
  • close callbacks (callbacks for closing events): Callbacks such as socket.on(‘close’,…) are called at this stage.
Between each run of the event loop, Node.js checks to see if it is waiting for asynchronous I/O or a timer, and automatically shuts down if not.

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

    If the poll queue is not empty, the event loop will execute the queue synchronously until the queue is empty or the executed callback reaches the system upper limit;
  • If the poll queue is empty, the following will happen:
  • - If the code has been set with a callback by setImmediate() , the event loop will enter the result poll phase into the check phase, and execute the queue in the check phase (the queue in the check phase is set by setImmediate).
    - If the code does not set setImmediate(callback), the event loop will block at this stage and wait for the callback to join the poll queue, and execute it immediately once it arrives.
If the event loop enters the poll phase, and the code sets the timer:

    If the poll queue enters the empty state (that is, the poll phase is idle), the event The loop will check timers. If one or more timers have arrived, the event loop will enter the timer phase in loop order and execute the timer queue.
[Recommended learning: "

nodejs tutorial"]

The above is the detailed content of What are the stages of the nodejs event loop?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template