Node.js事件循环理解(带图)

时间:2021-03-05 23:53:29

I've read this and this, watched this...

我读过这个,看过这个......

I've made a diagram of how I understand it:

我已经制作了一个如何理解它的图表:

Node.js事件循环理解(带图)

  • Javascript callbacks (functions) can be present in the current queue, check queue, close callbacks queue, timers queue and I/O callbacks queue.
  • Javascript回调(函数)可以出现在当前队列,检查队列,关闭回调队列,定时器队列和I / O回调队列中。
  • Js code gets executed only from the current queue one function (task/job) at a time.
  • Js代码一次仅从当前队列执行一个函数(任务/作业)。
  • Js code executed at the moment can add microtasks (jobs) to the current queue to be executed after itself and macrotasks (tasks) to the check queue. It can add tasks to other queues only inderectly by asking the API to do it.
  • 此时执行的Js代码可以将微任务(作业)添加到当前队列中,以便在其自身和macrotasks(任务)之后执行到检查队列。它可以通过要求API执行任务来直接向其他队列添加任务。
  • Idle, prepare phase is used for some internal node js business (maybe like garbage collection).
  • 空闲,准备阶段用于某些内部节点js业务(可能像垃圾收集)。
  • Poll phase polls threads from the thread pool and fills the queues with appropriate callbacks.
  • 轮询阶段轮询线程池中的线程,并使用适当的回调填充队列。
  • Idle, prepare and poll phases don't have queues for js callbacks associated with them.
  • 空闲,准备和轮询阶段没有与它们相关联的js回调的队列。
  • (four) Threads in the thread pool are all identical and have no specialization.
  • (四)线程池中的线程都是相同的,没有专门化。
  • Event loop takes and executes tasks one by one from each queue until it's empty then moves on to the next queue.
  • 事件循环从每个队列逐个接受并执行任务,直到它为空,然后移动到下一个队列。
  • Tasks in the queues don't have any jobs (microservices) associated with them. Jobs are created only during execution of a task or another job and are present only in the current task queue.
  • 队列中的任务没有与之关联的任何作业(微服务)。作业仅在执行任务或其他作业期间创建,并且仅在当前任务队列中存在。

Is that understanding right or am I missing something?

这种理解是正确的还是我错过了什么?

MS Power Point .pptx file with the diagram can be found here.

可以在此处找到带有图表的MS Power Point .pptx文件。

1 个解决方案

#1


1  

The diagram does seem quite complicated. I find a king analogy quite perfect in this context to have a grey level understanding about how event-loop works.

该图看起来确实很复杂。在这种情况下,我发现一个非常完美的国王类比,可以对事件循环的工作方式有一个灰度级的理解。

Imagine the code you want to run is a king and node is the army of servants.

想象一下,你想要运行的代码是一个国王,节点是仆人的军队。

The day starts by one servant waking up the king and asking him if he needs anything. The king gives the servant a list of tasks and goes back to sleep a little longer. The servant now distributes those tasks among his colleagues and they get to work.

这一天开始于一个仆人醒来的国王,问他是否需要任何东西。国王给仆人一份任务清单,然后再睡一会儿。仆人现在将这些任务分配给他的同事并开始工作。

Once a servant finishes a task, he lines up outside the kings quarter to report. The king lets one servant in at a time, and listens to things he reports. Sometimes the king will give the servant more tasks on the way out.

一旦仆人完成任务,他就会在国王区外排队报告。国王一次允许一个仆人,并听取他报告的事情。有时国王会在出路时给仆人更多的任务。

Life is good, for the king's servants carry out all of his tasks in parallel, but only report with one result at a time, so the king can focus.

生命是美好的,因为国王的仆人同时执行他的所有任务,但一次只报告一个结果,所以国王可以集中注意力。

The king here is the main node process. This is how the nodejs is said to be single-threaded yet asynchronous.

这里的国王是主要的节点过程。这就是nodejs被称为单线程但异步的方式。

#1


1  

The diagram does seem quite complicated. I find a king analogy quite perfect in this context to have a grey level understanding about how event-loop works.

该图看起来确实很复杂。在这种情况下,我发现一个非常完美的国王类比,可以对事件循环的工作方式有一个灰度级的理解。

Imagine the code you want to run is a king and node is the army of servants.

想象一下,你想要运行的代码是一个国王,节点是仆人的军队。

The day starts by one servant waking up the king and asking him if he needs anything. The king gives the servant a list of tasks and goes back to sleep a little longer. The servant now distributes those tasks among his colleagues and they get to work.

这一天开始于一个仆人醒来的国王,问他是否需要任何东西。国王给仆人一份任务清单,然后再睡一会儿。仆人现在将这些任务分配给他的同事并开始工作。

Once a servant finishes a task, he lines up outside the kings quarter to report. The king lets one servant in at a time, and listens to things he reports. Sometimes the king will give the servant more tasks on the way out.

一旦仆人完成任务,他就会在国王区外排队报告。国王一次允许一个仆人,并听取他报告的事情。有时国王会在出路时给仆人更多的任务。

Life is good, for the king's servants carry out all of his tasks in parallel, but only report with one result at a time, so the king can focus.

生命是美好的,因为国王的仆人同时执行他的所有任务,但一次只报告一个结果,所以国王可以集中注意力。

The king here is the main node process. This is how the nodejs is said to be single-threaded yet asynchronous.

这里的国王是主要的节点过程。这就是nodejs被称为单线程但异步的方式。