简体   繁体   English

node js 事件循环中的微任务在哪里执行,它的阶段是如何优先级排序的?

[英]Where are microtasks executed in the node js event loop and how are its phases prioritised?

This is the Node event loop.这是节点事件循环。

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

I know that Node uses the V8 JS engine and I've learned that V8 has its own event loop that embedders are free to replace or extend .我知道 Node 使用 V8 JS 引擎,并且我了解到V8 有自己的事件循环,嵌入器可以自由替换或扩展 In Chrome for example, there is a macrotask and a microtask queue (the microtask queue has priority over the macrotask queue) and the event loop pushes callbacks from those queues once the call stack of the main thread is empty.例如,在 Chrome 中,有一个宏任务和一个微任务队列(微任务队列优先于宏任务队列),一旦主线程的调用堆栈为空,事件循环就会从这些队列中推送回调。

You can queue microtasks on the microtask queue manually using the queueMicrotask function.您可以使用queueMicrotask function 手动将微任务排队到微任务队列中。

I've tried using this function in Node as well and it works.我也试过在 Node 中使用这个 function 并且它可以工作。 My question is , where are microtasks queued on the event loop?我的问题是,事件循环中的微任务在哪里排队? In what phase are they queued in?他们在哪个阶段排队? Do they have a phase that is specific for them or are they mixed up with other kinds of callbacks?它们是否有特定于它们的阶段,或者它们是否与其他类型的回调混合在一起?

I've tried this code to get an idea of what has priority in the Node event loop:我试过这段代码来了解节点事件循环中的优先级:

const hello_world =() => {
    console.log("Hello World 2");
};

queueMicrotask(hello_world);

setTimeout(
    () => {
        console.log("Hello World 3");
    },
    0
);

console.log("Hello World 1");

/*
OUTPUT:

Hello World 1
Hello World 2
Hello World 3
*/

So, I can see that microtasks have priority over timers (which are considered macrotasks in the chrome event loop) but where are the microtasks enqueue in the event loop in Node ?因此,我可以看到微任务优先于计时器(在 chrome 事件循环中被视为宏任务),但是在 Node 的事件循环中,微任务在哪里排队

And furthermore, what has priority over what in the Node event loop?此外,在 Node 事件循环中,什么优先于什么? I know that in Chrome for example, the microtask queue has priority over the macrotask queue, but what phase has priority over what phase in the Node event loop?我知道例如在 Chrome 中,微任务队列优先于宏任务队列,但是哪个阶段优先于节点事件循环中的哪个阶段?

Where are the microtasks enqueue in the event loop in Node? Node 的事件循环中的微任务在哪里排队?

They can be enqueued anywhere - you probably meant to ask where they are dequeued:-)他们可以在任何地方排队——你可能想问他们在哪里排队:-)

The article you've read answers this: 你读过的文章回答了这个问题:

The event loop should process the micro-task queue entirely, after processing one macro-task from the macro-task queue.在处理完宏任务队列中的一个宏任务之后,事件循环应该完全处理微任务队列。 […] But in NodeJS versions before v11.0.0 , the micro-task queue is drained only in between each two phases of the event loop. […] 但是在 v11.0.0 之前的 NodeJS 版本中,微任务队列仅在事件循环的每两个阶段之间被耗尽。

All those "phases" in your diagram are for processing macro tasks: timers, pending tasks, polled events, immediates, close events. 图表中的所有这些“阶段”都用于处理宏任务:计时器、待处理任务、轮询事件、立即事件、关闭事件。 The microtasks are executed after each of those macrotasks, just like in the browser.微任务在每个宏任务之后执行,就像在浏览器中一样。

The only exception weirdness is the special case of process.nextTick , which does not enqueue a macrotask either.唯一的异常怪异是process.nextTick的特殊情况,它也不会将宏任务排入队列。 This article distinguishes between a "promises microtask queue" and a " nextTick microtask queue" - the overarching term "microtask" being used for everything that comes before the next macrotask. 本文区分了“promises 微任务队列”和“ nextTick微任务队列”——总体术语“微任务”用于表示下一个宏任务之前的所有内容。

What phase has priority over what phase in the Node event loop?哪个阶段优先于 Node 事件循环中的哪个阶段?

There is no "priority".没有“优先级”。 As the diagram shows nicely, they are executed one after the other, repeatedly, always in the same order, ad nauseam.如图所示,它们一个接一个地重复执行,总是以相同的顺序执行,令人作呕。 Some phases have limits on the number of tasks to execute at once, to prevent starving the other phases, then the remaining tasks are run simply at the next time when it's the phase's turn again.有些阶段对一次执行的任务数量有限制,以防止其他阶段挨饿,然后在下一次轮到该阶段时简单地运行剩余的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js事件循环阶段 - Node.js Event Loop Phases 为什么Node.js事件循环需要多个阶段? - Why does the Node.js event loop require multiple phases? 对 Nodejs 事件循环中的计时器和微任务感到困惑 - Confused about timers and microtasks in Nodejs event loop 即使事件循环中没有要执行的回调,Node.js Web服务器如何保持运行? - How does A Node.js Web Server Keep Running Even When There Are No Callbacks To Be Executed In The Event Loop? 为什么事件循环对微任务没有超时? - Why doesn't event loop have timeout for microtasks? Node.JS中的事件循环如何可预测 - How predictable is the event loop in Node.JS 在I / O回调之前执行Node.js setImmediate(事件循环) - Node.js setImmediate executed before I/O callbacks (Event Loop) 在setInterval(()=&gt; {},5000)和setTimeout(()=&gt; {},5000)之间的Node.js / Javascript事件循环中将首先执行什么? - What will be executed first in Node.js/Javascript Event Loop among setInterval(()=>{}, 5000) and setTimeout(()=>{}, 5000)? 如何使用承诺等待函数执行以等待其在 Javascript/Node JS 中的回调 - How to wait for a function to be executed using promise to wait on its callback in Javascript/Node JS Node JS中的事件循环和EventEmitter - Event Loop and EventEmitter in Node JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM