在节点中等待回调时运行任意代码? [英] Run Arbitrary Code While Waiting For Callback in Node?

查看:105
本文介绍了在节点中等待回调时运行任意代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在是Node的初学者。我理解回调阻止Node在等待I / O或其他进程时阻塞,但是Node在等待I / O完成时做什么?我在nodejitsu上阅读的一个教程说,回调是在完成给定任务时调用的函数;这可以防止任何阻塞,并允许其他代码在此期间运行。但是它运行什么其他代码?让我们说,给定块的其他代码依赖于从数据库检索的信息。在等待回调时,Node可以处理其他连接,还是只能在当前块中运行其他代码?

I'm a beginner in Node right now. I understand that callbacks prevent Node from blocking while waiting for I/O or some other process, but what is Node doing while waiting for that I/O to finish? One tutorial I read on nodejitsu says, " A callback is a function called at the completion of a given task; this prevents any blocking, and allows other code to be run in the meantime." but what other code is it running? Let's say the other code a given block is dependent on the information being retrieved from the database. Can Node go handle other connections while waiting for the callback or can it only run other code in the current block?

推荐答案

关闭事件队列。当您启动异步操作时,该操作将独立于后台中的节点进行。节点继续运行当前执行线程中的其余代码,直到它到达执行线程的末尾(例如,一切返回到堆栈的顶部)。当节点运行当前执行线程时,没有其他执行线程在节点中运行(忽略生成器和光纤)。这就是为什么人们将它称为单线程。它顺序运行一个特定的执行线程,而不是并行执行多个线程。

Node works off an event queue. When you start an async operation, that operation proceeds independently from node in the background. Node continues running the rest of your code in the current execution thread until it gets to the end of that execution thread (e.g. everything returns back up to the top of the stack). While node is running a current execution thread, no other threads of execution will run in node (ignoring for the moment generators and fibers). This is why people refer to it as single threaded. It runs a particular thread of execution one at a time sequentially, not multiple threads of execution in parallel. One must finish before the next one runs.

当一个执行线程完成后,如果事件队列中还有其他事件,那么节点将弹出下一个事件队列并运行它。事件队列中的其他事件可以来自任何事物。它们可以来自计时器,从刚刚启动的异步事件或从程序中其他地方的异步事件(例如在您的特定问题中完成的其他数据库操作)。如果没有其他事件(因此没有代码准备好运行),那么节点只是等待直到下一个事件发生(除了可能的垃圾回收之外什么都不做)。

When one thread of execution finishes, if there are other events in the event queue, then node will pop the next event off the queue and run it. Other events in the event queue can be from anything. They can be from timers, from the async event you just start or from async events elsewhere in your program (e.g. other database operations finishing in your specific question). If there are no other events (so thus no code ready to be run), then node just waits until the next event occurs (doing nothing except perhaps garbage collection).

然后,当异步操作完成(在后台)时,它将向事件队列添加一个事件,以调用它的回调。如果当时没有其他任何东西当前在节点中运行,那么异步回调事件将运行。如果节点中当前正在运行其他任何东西,那么该操作将完成执行,并且当它正在运行时,事件队列中的下一个事件将运行,等等...

Then, when the async operation completes (in the background), it will add an event to the event queue to call it's callback. If nothing else is currently running in node at that time, then that async callback event will run. If something else is currently running in node, then that operation will finish executing and when it does, the next event in the event queue will run and so on...

以这种方式,在nodej中每次只有一个执行线程。当一个完成时,nodejs执行引擎从事件队列获取下一个事件并运行它。当异步操作完成并想要调用其回调时,它们会将事件插入事件队列。

In this way, there is only ever one thread of execution at a time in nodejs. When one finishes, the nodejs execution engine gets the next event off the event queue and runs it. When async operations finish and want to call their callback, they insert an event into the event queue.

这个回答描述事件队列并包含一些其他引用是为浏览器写的,但几乎所有相同的规则适用于nodejs,因此它也可以帮助您更多地了解这一点。

This answer which describes the event queue and contains some other references was written for a browser, but pretty much all the same rules apply in nodejs so it might also help you understand more about this.

这篇关于在节点中等待回调时运行任意代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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