NodeJS 事件循环的确切处理是什么? [英] What is the exact handling of the NodeJS Event Loop?

查看:27
本文介绍了NodeJS 事件循环的确切处理是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 NodeJS 事件循环从事件队列中收集任务并将控制权转移到任务的回调.任务完成后,任务从事件循环中转移控制权.

I know that NodeJS Event Loop collects Tasks from Event Queue and transfers control to the callback of the Task. When the task is completed, the Task transfers control from Event Loop.

因此,我认为实际上返回回调是一个从 Task 获得控制权的 Event Loop.

Therefore, I think that actually returning the callback is an Event Loop that has received control right from the Task.

这是正确的想法吗?

另外,如果关于 Event Loop 中可能发生的阻塞现象的假设是正确的,如果有一个延迟的异步任务,事件循环是否可以在等待任务返回控制权的同时处理其他任务?

Also, if the assumption is correct about the blocking phenomenon that can occur in the Event Loop, if there is a delayed asynchronous task, can the event loop process other tasks while waiting for control to be returned from the task?

或者这个假设是错误的,所以事件循环在延迟的异步任务完成之前不起作用?

Or is that assumption incorrect, so the Event Loop doesn't work until the delayed asynchronous task is completed?

在 async await 的情况下,我想知道 await 是否停止了 Event Loop.

In the case of async await, I wonder if await stops the Event Loop.

推荐答案

您可以将事件循环视为一个实际的循环,例如:

You can think of the Event Loop as an actual loop, something like:

let event_queue = [compiled_toplevel_code];
while (true) {
  if (event_queue.length === 0) {
    sleepUntilWokenUp();
  }
  if (event_queue.length > 0) {
    let callback = event_queue.shift();
    callback();
  }
}

其中 sleepUntilWokenUp() 是一个特殊的函数,它会挂起当前线程,直到另一个线程发送一些信号来唤醒它.异步操作(如文件系统或网络访问)由此类其他线程处理.当他们有一个回调准备好执行时,他们会将它排入队列,然后发送适当的唤醒信号.

where sleepUntilWokenUp() is a special function that suspends the current thread until another thread sends some signal to wake it up. Async operations (like file system or network access) are handled by such other threads. When they have a callback ready to execute, they will enqueue it and then send the appropriate wake-up signal.

您可以将 setImmediate(callback) 想象为实现为 event_queue.push(callback).

You can imagine setImmediate(callback) as being implemented as event_queue.push(callback).

传输控制"从事件循环到回调仅仅意味着调用回调;回归控制"到事件循环只是意味着回调函数返回.

"Transferring control" from the event loop to a callback simply means calling the callback; "returning control" to the event loop simply means that the callback function returns.

长时间运行的异步"任务总是分解成在主线程上运行的片段,安排一些要完成的工作(通常在另一个线程上),然后返回.一旦请求的任务在后台完成,它们相应的回调就会被排队.甚至同步"带有 await 的调用只是将函数分成两半的语法糖,这样前半部分会运行完成,而后半部分会在等待任务时推送到 event_queue已经完成.

Long-running "async" tasks always decompose into snippets that run on the main thread, schedule some work to be done (typically on another thread), then return. Once the requested task has been completed in the background, their corresponding callback is enqueued. Even "synchronous" calls with await are just syntactic sugar for splitting the function in two halves such that the first half runs to completion, and the second half is pushed onto the event_queue when the awaited task has completed.

现实中的完整情况稍微复杂一些(在 Node 文档中有详细描述),不同种类的事物有几个不同的队列,但以上描述了总体思路.

The full situation in reality is somewhat more complicated (and described in detail in the Node documentation) with a few different queues for different kinds of things, but the above describes the general idea.

这篇关于NodeJS 事件循环的确切处理是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆