ReasonJun

Node.js : How make asynchronous L/O (event loop) 본문

Backend/Node.js

Node.js : How make asynchronous L/O (event loop)

ReasonJun 2023. 9. 4. 00:29
728x90

In Node.js, the event loop is a fundamental part of its architecture and is crucial for handling asynchronous operations efficiently. It allows Node.js to perform I/O operations and other tasks without blocking the execution of other code. Understanding the event loop is essential for writing scalable and performant Node.js applications.

 

Here's an overview of how the event loop works in Node.js:

  1. Event Queue: Node.js is designed to be non-blocking and asynchronous. When you perform asynchronous operations, such as reading a file, making an HTTP request, or interacting with a database, these operations are initiated, and their callbacks are placed in an event queue.
  2. Event Loop: The event loop is a continuously running process that checks the event queue for pending events (callbacks). It's responsible for executing these callbacks one by one in a loop. The event loop operates single-threadedly, meaning it can only execute one callback at a time.
  3. Execution of Callbacks: When an event loop iteration starts, it checks if there are any pending callbacks in the event queue. If there are, it picks the first one and executes it. The callback can contain I/O operations, timers, or other asynchronous tasks.
  4. Non-blocking Nature: The event loop's key feature is its non-blocking nature. When an asynchronous operation is initiated, Node.js doesn't wait for it to complete. Instead, it continues executing other code. When the operation is finished, its callback is placed in the event queue, and the event loop eventually processes it.
  5. Event Loop Phases: The event loop is divided into multiple phases, each responsible for a different set of tasks, such as timers, I/O operations, and callbacks. These phases ensure that the event loop processes events in a specific order. The phases include:
    • Timers Phase: Handles timer-related callbacks scheduled using functions like setTimeout and setInterval.
    • Pending Callbacks Phase: Executes callbacks related to system events, such as network or file I/O.
    • Idle, Prepare Phases: Internal phases used for optimizing and preparing the event loop.
    • Poll Phase: Handles I/O operations, including reading from sockets and files. If there are pending I/O events, the event loop will stay in this phase.
    • Check Phase: Executes setImmediate callbacks.
    • Close Callbacks Phase: Executes callbacks registered with the close event.
  6. Exiting the Event Loop: The event loop continues running until there are no more events in the queue or if it's explicitly terminated. Once there are no more pending events, the Node.js process can exit.

https://www.simform.com/blog/what-is-node-js/

https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick

 

The Node.js Event Loop, Timers, and process.nextTick() | Node.js

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.

nodejs.org

 

728x90

'Backend > Node.js' 카테고리의 다른 글

setTimeout, setImmediate, process.nextTick()  (0) 2023.09.04
Single Thread / Multi Thread  (0) 2023.09.04
Node.js : Blocking / non-Blocking  (0) 2023.09.03
Node.js : bindings  (0) 2023.09.02
In Node.js, the relationship between V8 and libuv  (0) 2023.09.02
Comments