简体   繁体   English

Node js的回调队列排序

[英]Callback queue ordering of Node js

Example 1: 范例1:

console.log('Starting app');

setTimeout(() => {
  console.log('callback 1');
}, 2000);

sleep(4000);

setTimeout(() => {
  console.log('callback 2');
}, 1000);

console.log('Finishing up');

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

//Its Output
Starting app
Finishing up
Callback 1
Callback 2

Example 2 : 范例2:

console.log('Starting app');

setTimeout(() => {
  console.log('callback 1');
}, 2000);

sleep(4000);

setTimeout(() => {
  console.log('Callback 2');
}, 0);

console.log('Finishing up');

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

//Its Output
Starting app
Finishing up
Callback 2
Callback 1

I am trying to understand that two asyncs function timer start executing simultaneously or one by one. 我试图了解两个异步函数计时器开始同时开始执行或一个接一个地执行。
Example 1 is working fine as i expected. Example 1如我预期的那样工作正常。 Callback 1 reached in queue first because it has timeout of 2 seconds. 回调1首先到达队列,因为它的超时时间为2秒。 because there delay is for 4 seconds before the Callback 2. 因为在回调2之前有4秒的延迟。

Example 2 this example is not working as i expected. Example 2此示例无法正常工作。 Callback 1 should be reached in queue first because it has timeout of 2 seconds and delay of 4 seconds before the Callback 2. 应该先在队列中到达回调1,因为它的超时为2秒,延迟为回调2前的4秒。

It appears the assumption that the loop for the sleep function won't finish before the timeout might be the issue. 似乎假设sleep功能的循环在超时可能成为问题之前不会完成。

 console.log('Starting app'); sleep(4000); console.log('Finishing app'); function sleep(milliseconds) { var start = new Date().getTime(); for (var i = 0; i < 1e7; i++) { if ((new Date().getTime() - start) > milliseconds){ console.log('breaking'); break; } } console.log(`Sleep time: ${new Date().getTime() - start}ms`); } 

The time for the loop to complete is dependent on the computer it is running on but most modern computers will never break since it finishes faster than the time allocated. 循环完成的时间取决于运行的计算机,但是大多数现代计算机永远都不会中断,因为它完成的时间比分配的时间快。 This creates a range of "sleep" times which can lead to getting different outputs on different machines (even different on the same machine between runs if the deltas are small enough). 这会产生一定范围的“睡眠”时间,这可能导致在不同的机器上获得不同的输出(如果增量足够小,则两次运行之间同一台机器上的输出也会不同)。

For instance, on one of my machines it takes ~800ms (a range of 750ms to 830ms) for this code to complete. 例如,在我的一台机器上,此代码需要花费约800毫秒(750毫秒至830毫秒的范围)来完成。 Much shorter than the 4000ms specified. 比指定的4000ms短得多。

So in the first test example the code would run like so on my computer: 因此,在第一个测试示例中,代码将在我的计算机上按以下方式运行:

  • sets a timeout of 2000ms for outputting "callback 1" 设置2000ms的超时时间以输出“回调1”
  • loops for ~800ms (NOT 4000ms as desired) 循环约800毫秒(不需要4000毫秒)
  • sets a timeout of 1000ms for outputting "callback 2" 设置输出“回调2”的超时时间为1000ms

This results in "callback 2" being printed at ~1800ms and "callback 1" being printed at ~2000ms. 这导致“回调2”在约1800ms处打印,而“回调1”在约2000ms处打印。 Given how close those are, I would venture the machine you are using is a bit slower executing the loop so you observed it they way you have indicated. 考虑到它们之间的距离,我敢冒险您正在使用的机器执行该循环要慢一些,因此您可以按照指示的方式观察它。

With the second test example, the code would execute as such on my computer: 在第二个测试示例中,代码将在我的计算机上这样执行:

  • sets a timeout of 2000ms for outputting "callback 1" 设置2000ms的超时时间以输出“回调1”
  • loops for ~800ms (NOT 4000ms as desired) 循环约800毫秒(不需要4000毫秒)
  • sets a timeout of 0ms for outputting "callback 2" 将超时时间设置为0ms,以输出“回调2”

This results in "callback 2" being printed at ~800ms and "callback 1" being printed at ~2000ms. 这导致“回调2”在约800毫秒处打印,而“回调1”在约2000毫秒处打印。 Since your computer is fast enough to finish the loop in under 4s, you now observe the "unexpected" output as stated in your question. 由于您的计算机速度足够快,可以在4秒内完成循环,因此您现在可以观察到问题中指出的“意外”输出。

So, basically the loop you are using to "sleep" is not providing the wait time you are basing your expectations off of. 因此,基本上,您用来“睡眠”的循环并没有提供您基于期望的等待时间。

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

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