简体   繁体   English

事件循环处理的回调是否完成?

[英]Is the callback handled by the event loop completed?

I understood through NodeJS's Event Loop documentation that the callbacks handled by the event loop are completed tasks.我通过 NodeJS 的事件循环文档了解到,事件循环处理的回调是已完成的任务。

Is it correct that Libuv's thread handles asynchronous work, and when it's done, the event loop pulls the work from the event queue and just returns a callback? Libuv的线程处理异步工作是否正确,当它完成时,事件循环从事件队列中拉出工作并返回一个回调?

Because the callback I understand is the action to be performed after the job is done.因为我理解的回调是工作完成后要执行的动作。

If that assumption is correct, when can the event loop block?如果这个假设是正确的,事件循环什么时候可以阻塞?

Also, it doesn't make sense that the event queue and event loop are used just for callback execution after the job is done.此外,事件队列和事件循环仅用于作业完成后的回调执行是没有意义的。

What am I misunderstanding?我有什么误解?

Is it correct that Libuv's thread handles asynchronous work Libuv的线程处理异步工作是否正确

Some asynchronous work is handled by libuv threads, some is not.有些异步工作是由 libuv 线程处理的,有些则不是。 For example networking uses OS APIs that already work asynchronously and does not use or need threads.例如,网络使用的操作系统 API 已经异步工作并且不使用或不需要线程。 nodejs has its own timer implementation that does not use threads. nodejs 有自己的不使用线程的计时器实现。

and when it's done, the event loop pulls the work from the event queue and just returns a callback?完成后,事件循环从事件队列中提取工作并返回一个回调?

When an asynchronous job is done, it inserts an event into the event queue.当异步作业完成时,它会将一个事件插入到事件队列中。 When it is that event's turn to get pulled from the event queue, then it will trigger a callback to get called.当轮到该事件从事件队列中被拉出时,它将触发回调以被调用。

If that assumption is correct, when can the event loop block?如果这个假设是正确的,事件循环什么时候可以阻塞?

The event loop is blocked anytime a callback from the prior event is still executing.只要前一个事件的回调仍在执行,事件循环就会被阻塞。 Events are processed one at a time.一次处理一个事件。

Also, it doesn't make sense that the event queue and event loop are used just for callback execution after the job is done.此外,事件队列和事件循环仅用于作业完成后的回调执行是没有意义的。

I'm not sure exactly where your confusion is here.我不确定您的困惑到底在哪里。 When an event is processed a callback is called and it starts to run.处理事件后,将调用回调并开始运行。 If the work that is being done has further asynchronous work, then that callback will start one or more additional asynchronous operations and then it will return back to the event queue (the callback will return).如果正在完成的工作有进一步的异步工作,那么该回调将启动一个或多个额外的异步操作,然后它将返回到事件队列(回调将返回)。

At this point, the work that operation wants to complete is not yet fully done (there are some new outstanding asynchronous operations), but as far as the event loop is concerned, that callback returned and that callback itself is done so the event loop finds the next event ready to be run.此时,操作想要完成的工作尚未完全完成(有一些新的未完成的异步操作),但就事件循环而言,回调返回并且回调本身已完成,因此事件循环找到准备运行下一个事件。

At some future point, those asynchronous operations that were just started will finish and will insert an event into the event loop when they get called that higher level operation will make further progress toward its eventual goal.在未来的某个时间点,那些刚刚开始的异步操作将完成,并在它们被调用时将一个事件插入事件循环,更高级别的操作将朝着其最终目标取得进一步进展。 So, in this way, a series of asynchronous operations in service of some higher level operation do not block the event loop during the whole duration, only during small slivers of execution along the way with opportunities for other things to run when the larger operation is waiting for asynchronous operations to complete.因此,通过这种方式,为某些更高级别操作服务的一系列异步操作不会在整个持续时间内阻塞事件循环,只会在沿途的一小段执行过程中,当更大的操作有机会运行其他事情时等待异步操作完成。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM