I've read this and this , watched this ...
I've made a diagram of how I understand it:
current queue
, check queue
, close callbacks queue
, timers queue
and I/O callbacks queue
. current queue
one function (task/job) at a time. 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. thread pool
are all identical and have no specialization. current task queue
. Is that understanding right or am I missing something?
MS Power Point .pptx file with the diagram can be found here .
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.