Chrome浏览器事件循环与节点事件循环之间是否存在显着差异? [英] Are there significant differences between the chrome browser event loop versus the node event loop?

查看:94
本文介绍了Chrome浏览器事件循环与节点事件循环之间是否存在显着差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Philip Roberts在解释浏览器事件循环此处方面做得非常出色,在调用堆栈,事件循环,任务队列之间提供了清晰的解释,然后像webapis这样的外部线程。我的问题是这些与Node事件循环中的等效组件并行,并且它们被称为基本相同的东西。也就是说,当我使用Node的文件和Web i / o库进行调用时,这些是在堆栈外发生的事情,其回调在任务队列中排队?

Philip Roberts does a brilliant job explaining the browser event loop here providing a clear explanation between the call stack, event loop, task queue, and then "outside" threads like webapis. My question is do these parallel the equivalent components in the Node event loop and are they called basically the same thing. That is, when I make a call using Node's file and web i/o libraries, these are things that happen outside the stack whose callbacks are queued in a task queue?

推荐答案


...当我使用Node的文件和Web i / o库进行调用时,这些是在堆栈外发生的事情,其回调在任务中排队队列?

...when I make a call using Node's file and web i/o libraries, these are things that happen outside the stack whose callbacks are queued in a task queue?

是的,绝对的;它们像Ajax一样异步,而 setTimeout 是异步的。它们在调用堆栈之外执行一些操作,当它们完成该操作时,它们将事件添加到队列以由事件循环处理。

Yes, absolutely; they're asynchronous just like Ajax and setTimeout are asynchronous. They perform some operation outside of the call stack, and when they've finished that operation, they add an event to the queue to be processed by the event loop.

Node's API提供了一种异步无操作, setImmediate 。对于该函数,我上面提到的一些操作是什么都不做,之后一个项目立即被添加到事件队列的末尾。

Node's API provides a kind of asynchronous no-op, setImmediate. For that function, the "some operation" I've mention above is "do nothing", after which an item is immediately added to the end of the event queue.

有一个更强大的 process.nextTick ,它将事件添加到事件队列的前端,有效地排除并进行所有其他排队事件等待。如果以递归方式调用,这可能会导致其他事件延迟(直到达到 maxTickDepth )。

There is a more powerful process.nextTick which adds an event to the front of the event queue, effectively cutting in line and making all other queued events wait. If called recursively, this can cause prolonged delay for other events (until reaching maxTickDepth).

这篇关于Chrome浏览器事件循环与节点事件循环之间是否存在显着差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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