简体   繁体   English

nodejs 事件循环如何在没有 for/while 永远循环的情况下继续运行?

[英]How does nodejs event loop keep running without a for/while forever loop?

I read Nodejs Event Loop and "Event Loop Explained" and Don't Block the Event Loop我阅读了Nodejs 事件循环“事件循环解释”并且不要阻塞事件循环

I don't think there is a for/while forever loop in nodejs code (js or c++), eg as here explains libev event loop http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#EXAMPLE_PROGRAM我认为 nodejs 代码(js 或 c++)中没有 for/while 永远循环,例如这里解释了 libev 事件循环http://pod.tst.eu/http://cvs.schmorp.de/libev /ev.pod#EXAMPLE_PROGRAM

int
   main (void)
   {
     // use the default event loop unless you have special needs
     struct ev_loop *loop = EV_DEFAULT;

    // init

    // now wait for events to arrive
    ev_run (loop, 0);

    // break was called, so exit
    return 0;
 }

So how does nodejs event loop run forever or maybe there is indeed a for/while forever loop, as https://en.wikipedia.org/wiki/Event_loop pseudo code shows ?那么 nodejs 事件循环如何永远运行,或者确实存在 for/while 永远循环,如https://en.wikipedia.org/wiki/Event_loop伪代码所示?

I searched all SO sites and find Is an event loop just a for/while loop with optimized polling?我搜索了所有 SO 站点,发现事件循环是否只是具有优化轮询的 for/while 循环? . . There are different opinions there, eg Bryan Oakley's answer said yes and other said no.那里有不同的意见,例如 Bryan Oakley 的回答是肯定的,而其他人的回答是否定的。

But my question is a little bit different from that one.但我的问题与那个有点不同。 I was wondering without a for/while loop, how does nodejs event loop keeps running?我想知道没有 for/while 循环,nodejs 事件循环如何继续运行?

Why would it use "while true" loop?为什么要使用“while true”循环? Even though it's commonly stated that "it's an infinite loop", that doesn't mean it's an actually while loop.尽管通常说“这是一个无限循环”,但这并不意味着它实际上是一个while循环。

As you've read in "Event Loop Explained", there are different phases in the event loop cycle.正如您在“事件循环解释”中读到的那样,事件循环周期有不同的阶段。 After it's done with the last phase, it basically sleeps itself for some short period of time and starts from the first phase again.完成最后一个阶段后,它基本上会自己休眠一小段时间,然后再次从第一阶段开始。 It's commonly known that in browsers, using setTimeout(callback, 0) actually executes in 4 or more milliseconds, meaning there's a gap between the cycles of at least 4ms.众所周知,在浏览器中,使用setTimeout(callback, 0)实际上在4 or more毫秒4 or more毫秒内执行,这意味着至少 4ms 的周期之间存在差距。

I think your biggest misunderstanding here is taking the words too literally.我认为你在这里最大的误解是过于字面理解这些词。 Bryan says "From a conceptual point of view, all event loops are essentially", which doesn't mean there's an actual while loop there. Bryan 说“从概念的角度来看,所有事件循环本质上都是”,这并不意味着那里有实际的 while 循环。 It's way more complex and uses OS internal scheduling (kernel) to wait for some period of time.它更复杂,并使用操作系统内部调度(内核)来等待一段时间。 I'd say a better example (still way way too far from actual implementation) would be:我想说一个更好的例子(离实际实现还差太远)是:

startEventLoopPhaseExecution() {
  processEventLoopPhases();
  restABit(); // synchronously do nothing for some time
  startEventLoopPhaseExecution(); // start from phase 1 again
}

With Andrey's input and this article Event loop in JavaScript I further check libuv's source code, it is indeed a while loop,有了Andrey的输入和这篇文章Event loop in JavaScript,我进一步查看了libuv的源码,​​确实是一个while循环,

https://github.com/libuv/libuv/blob/v1.x/src/unix/core.c#L369 https://github.com/libuv/libuv/blob/v1.x/src/unix/core.c#L369

int uv_run(uv_loop_t* loop, uv_run_mode mode) {
 ...
  r = uv__loop_alive(loop);
  if (!r)
    uv__update_time(loop);

  while (r != 0 && loop->stop_flag == 0) {

And https://github.com/libuv/libuv/blob/v1.x/src/win/core.c#L596 is basiclly the samehttps://github.com/libuv/libuv/blob/v1.x/src/win/core.c#L596基本相同

int uv_run(uv_loop_t *loop, uv_run_mode mode) {
   ...
   while (r != 0 && loop->stop_flag == 0) {

I was confused whether nodejs uses the event loop implemented in libev or libuv at first, but that is another topic.起初我很困惑 nodejs 是使用 libev 还是 libuv 中实现的事件循环,但这是另一个话题。

I checked libev source code for ev_run https://github.com/enki/libev/blob/master/ev.c#L3547我检查了 ev_run 的 libev 源代码https://github.com/enki/libev/blob/master/ev.c#L3547

It is also a do while loop so I guess I just asked a silly question.这也是一个do while循环,所以我想我只是问了一个愚蠢的问题。

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

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