The Node.js Event Loop, Timers, and process.nextTick() Node.js事件循环,定时器和process.nextTick()

个人翻译

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

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

What is the Event Loop?

什么是事件循环圈?

The event loop is what allows Node.js to perform non-blocking I/O operations — despite the fact that JavaScript is single-threaded — by offloading operations to the system kernel whenever possible.

Since most modern kernels are multi-threaded, they can handle multiple operations executing in the background. When one of these operations completes, the kernel tells Node.js so that the appropriate callback may be added to the poll queue to eventually be executed. We'll explain this in further detail later in this topic.

事件循环允许了nodejs来实现非阻塞I/O操作——尽管JavaScript是单线程的——通过向操作系统内核(分流)操作。

大多数现代的内核都是多线程的,他们能在后台处理多线程操作。当某一个操作完成的时候,内核通知Nodejs,因此合适的回掉函数会被放到轮询队列中最终被执行,我们会在这篇文章的后面部分详细的解释这些内容。

Event Loop Explained

When Node.js starts, it initializes the event loop, processes the provided input script (or drops into the REPL, which is not covered in this document) which may make async API calls, schedule timers, or call process.nextTick(), then begins processing the event loop.

The following diagram shows a simplified overview of the event loop's order of operations.

事件循环的解释:

当Node.js运行的时候,它会初始化事件循环圈,处理当前的脚本(或者是在REPL中,这篇文章不涉及此方面内容),可能会调用一些异步的API、定时器、或者调用process.nextTick(),然后开始处理event loop,

下面图表展示了事件循环中各种操作顺序的一个简单概况。

   ┌───────────────────────────┐
┌─>│           timers          │
│  └─────────────┬─────────────┘
│  ┌─────────────┴─────────────┐
│  │     pending callbacks     │
│  └─────────────┬─────────────┘
│  ┌─────────────┴─────────────┐
│  │       idle, prepare       │
│  └─────────────┬─────────────┘      ┌───────────────┐
│  ┌─────────────┴─────────────┐      │   incoming:   │
│  │           poll            │<─────┤  connections, │
│  └─────────────┬─────────────┘      │   data, etc.  │
│  ┌─────────────┴─────────────┐      └───────────────┘
│  │           check           │
│  └─────────────┬─────────────┘
│  ┌─────────────┴─────────────┐
└──┤      close callbacks      │
   └───────────────────────────┘

note: each box will be referred to as a "phase" of the event loop.

注意:每个盒子都代表了事件循环的一个阶段。

Each phase has a FIFO queue of callbacks to execute. While each phase is special in its own way, generally, when the event loop enters a given phase, it will perform any operations specific to that phase, then execute callbacks in that phase's queue until the queue has been exhausted or the maximum number of callbacks has executed. When the queue has been exhausted or the callback limit is reached, the event loop will move to the next phase, and so on.

每个阶段都要执行一个先进先出的队列的回调。每个阶段都是唯一特殊的,一般来说,当event loop进入了某个阶段,它会执行那个阶段的所有操作,然后执行那个阶段队列里的回调直到队列结束,或者执行了最大限制值的回调函数数量。当队列结束或者回调数量限制达到了,event loop会进入下一个阶段,这样。

Since any of these operations may schedule more operations and new events processed in the poll phase are queued by the kernel, poll events can be queued while polling events are being processed. As a result, long running callbacks can allow the poll phase to run much longer than a timer's threshold. See the timers and poll sections for more details.

NOTE: There is a slight discrepancy between the Windows and the Unix/Linux implementation, but that's not important for this demonstration. The most important parts are here. There are actually seven or eight steps, but the ones we care about — ones that Node.js actually uses - are those above.

猜你喜欢

转载自www.cnblogs.com/eret9616/p/9134159.html