javascript - Node.js event loop understanding (with a diagram) - Stack Overflow

I've read this and this, watched this...I've made a diagram of how I understand it:Javascript

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

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

  • Javascript callbacks (functions) can be present in the current queue, check queue, close callbacks queue, timers queue and I/O callbacks queue.
  • Js code gets executed only from the current queue one function (task/job) at a time.
  • 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.
  • Idle, prepare phase is used for some internal node js business (maybe like garbage collection).
  • 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.
  • (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.

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

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

  • Javascript callbacks (functions) can be present in the current queue, check queue, close callbacks queue, timers queue and I/O callbacks queue.
  • Js code gets executed only from the current queue one function (task/job) at a time.
  • 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.
  • Idle, prepare phase is used for some internal node js business (maybe like garbage collection).
  • 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.
  • (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.

Share Improve this question edited Aug 8, 2017 at 12:38 grabantot asked Aug 8, 2017 at 10:57 grabantotgrabantot 2,14920 silver badges35 bronze badges 7
  • This looks overly plicated to me. Maybe it is, but isn't a simpler model sufficient when you write code? I think it is not necessary to know all the details going on under the hood. Isn't it sufficient to know that there is a queue for the current tick, a queue for the next tick and the callbacks awaiting the timer or I/O. – Lorenz Meyer Commented Aug 8, 2017 at 11:06
  • 8 I can't answer this, but I really appreciate the effort you put into your question. – georg Commented Aug 8, 2017 at 11:10
  • @LorenzMeyer A simpler model is sufficient to write code, but still I'd like to know what's under the hood. georg, thanks :) – grabantot Commented Aug 8, 2017 at 11:54
  • 3 I think the documentation part is the right place where you can post this. Actually you don't have a precise question anybody could answer, you just look for a peer review of your model. – Mario Santini Commented Aug 8, 2017 at 12:19
  • @MarioSantini Do you mean there stackoverflow./documentation/node.js/topics? Maybe I'll take a closer look at how it works tomorrow. Should I edit the existing Eventloop topic or create a new one? – grabantot Commented Aug 8, 2017 at 12:54
 |  Show 2 more ments

1 Answer 1

Reset to default 3

The diagram does seem quite plicated. 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745166688a4614676.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信