Dart事件队列和微任务 [英] Dart event queue and microtask

查看:63
本文介绍了Dart事件队列和微任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解dart事件循环的工作原理.我从事件循环和Dart 网站上阅读了事件循环文章,作者对此进行了解释很好,dart中的事件循环如何工作.

i am trying to understand, how dart event loop works. I read the event loop article from the website The Event Loop and Dart and the author explain pretty good how event loop in dart works.

但是我不明白的是,事件如何排队.例如

But what i don't understand is, how event get queue. For example

new Future(() => 21)
    .then((v) => v*2)
    .then((v) => print(v));

在这里,dart会在事件队列中创建三个条目还是仅创建一个?我知道,Future类负责延迟执行以及当我从中创建对象时

Will here dart gonna create three entries in the event queue or just one? I know, that the class Future is responsible for delay execution and when i create an object from it like

new Future(() => 21)

这只是事件循环中的一项.

it will be just one entry in the event loop.

在上面我已经提到的这篇文章中,我读到了有关微任务的信息.该微任务将在事件队列之前执行,但我看不出任何意义,为什么飞镖团队要实现此微任务?也许我需要一些例子!

In this article, that i have mentioned above, i read about microtask. This microtask is going to execute before event queue, but i don't see any sense, why dart team implement this microtask? Maybe i need some example!

推荐答案

经过一番调查,似乎正确的答案是它们将在下一个事件循环中执行"

After some investigation it appears that the right answer is "they will be executed in the next event loop"

要对其进行测试,您可以编写如下内容:

To test it you can write something like this:

import "dart:async";
void main() {
  new Future(() {
    scheduleMicrotask(()=>print("before loop 1"));
    print("in event loop");
  }).then((_) {
    scheduleMicrotask(()=>print("before loop 2"));
    print("in event loop");
  }).then((_) {
    scheduleMicrotask(()=>print("before loop 3"));
    print("in event loop");
  });
}

它应该输出:

在事件循环中
在事件循环中
在事件循环中
循环1之前
循环2之前
循环3之前

in event loop
in event loop
in event loop
before loop 1
before loop 2
before loop 3

尽管我不确定您能否打破这一优化.因此,唯一可以肯定的事实是,第一个 Future 将首先完成,第二个-第二个完成.

Although i'm not sure that you can't break this optimization. So only sure fact is that the firstFuture will complete first and the second - second.

奇怪的部分(已淘汰):

使用这样的代码:

import "dart:async";
void main() {
  new Future(() {
    scheduleMicrotask(print("before loop 1"));
    print("in event loop");
  }).then((_) {
    scheduleMicrotask(print("before loop 2"));
    print("in event loop");
  }).then((_) {
    scheduleMicrotask(print("before loop 3"));
    print("in event loop");
  });
}

输出为:

before loop 1
in event loop
before loop 2
in event loop
before loop 3
in event loop
Unhandled exception:
The null object does not have a method 'call'.

NoSuchMethodError: method not found: 'call'
Receiver: null
...

但是,这个:

import "dart:async";
void main() {
  new Future(() {
    scheduleMicrotask(()=>print("before loop 1"));
    print("in event loop");
  }).then((_) {
    scheduleMicrotask(()=>print("before loop 2"));
    print("in event loop");
  }).then((_) {
    scheduleMicrotask(()=>print("before loop 3"));
    print("in event loop");
  });
}

输出为:

在事件循环中
在事件循环中
在事件循环中
循环1之前
循环2之前
循环3之前

in event loop
in event loop
in event loop
before loop 1
before loop 2
before loop 3

我想我明白了.在第一个(错误版本)中, scheduleMicrotask 实际上从来没有得到正确的调度,但是由于Dart渴望执行参数,因此无论如何都会执行 print().因此,发生的是所有 Future 在下一个事件循环中执行并打印所有文本.这就是输出按调用顺序排列的原因:

I think i got it. In the first(wrong version) scheduleMicrotask actually never got properly scheduled, but since Dart has eager argument execution it executes print() anyway. So what happens is that all Future getting executed in the next event loop and print all text. That's why output is in call order:

在循环1之前
在事件循环中
循环2之前
在事件循环中
循环3之前
在事件循环中

before loop 1
in event loop
before loop 2
in event loop
before loop 3
in event loop

,而不是按计划顺序.

这篇关于Dart事件队列和微任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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