
Flutter: Futures, Isolates, Event Loop - yannikyeo
https://www.didierboelens.com/2019/01/futures---isolates---event-loop/
======
matt2000
This is a great writeup, wish I had it when I started with Flutter. BTW, I've
written a couple Flutter apps now and this set of tools (Futures, async/await,
Isolates) covers my multithreading use cases in a really nice and safe way.

By comparison, Grand central dispatch is pretty nice on iOS, but I really miss
the first class async/await calls. Side note: How did they make an entirely
new language for app development (Swift) and not build in async/await support?

Kotlin co-routines seem to be a pretty nice solution on Android, but before
that it was a bit of a mess. There was 4 or 5 different ways to do things
async, they all interact in super complicated ways with the activity life
cycle, and basically nothing is quite what you need.

Side side note: Unity also does a really nice version of all of this with a
single event loop and really powerful co-routines. Worth checking out
sometime.

~~~
coldtea
> _By comparison, Grand central dispatch is pretty nice on iOS, but I really
> miss the first class async /await calls. Side note: How did they make an
> entirely new language for app development (Swift) and not build in
> async/await support?_

By building it gradually and not rushing everything from the first day.

That said:

[https://gist.github.com/lattner/429b9070918248274f25b714dcfc...](https://gist.github.com/lattner/429b9070918248274f25b714dcfc7619)

~~~
fauigerzigerk
I think this is a question of priorities rather than rushing things. Swift is
a post Moore's Law language after all.

------
pests
I've been getting into Flutter and Dart recently after being a long time React
fan. I must say I like many things Flutter does differently. Especially around
its Material UI package (built-in) which is miles above any other web material
component library in any framework (React, Vue, Angular, etc.)

I must say wrapping my head around the addition of 'await' and the changes it
made to the EventLoop processing was difficult but as I type this comment I
realize I do fully grok it now. I must say that this is IMO probably the most
important piece of info in the article.

Basically,

"`await` causes immediate execution of the Future (up to the first `await` in
the Future) verses appending the Future to the end of the EventLoop queue"

If I have the above wrong someone please call me out.

Great article.

~~~
tylerhou
Await doesn’t immediately execute the future; see the example code in the
article.

It looks like the Dart concurrency model is basically the same as JS except
with microtasks and replacing “worker” with “isolate.”

~~~
pests
I'm not sure I agree.

In the first example `await` is added before the Future creation and this
causes immediate execution in the printed output (second version). In the
first version both Futures are pushed onto the EventTask queue and are
executed after main() has finished.

In the second example with method1 and method2, in method1 with async (which
signals a Future) on the forEach, all three Futures are pushed to the
EventLoop queue and execute after method1() has finished. In method2() there
is no new Future being added and execution happens immediately with the end of
method2 executing after all delayedPrints.

You also see the effect with all three Futures resolving at once in the
method1 case due to all three being added to the queue at the same time each
with the same delay. Whereas with method2 each print with a delay after due to
this function being synchronous with nothing going on the EventLoop.

~~~
tylerhou
Await pushes your task onto the end of the event queue; this might seem like
you’re immediately executing a future in many cases but it’s not true in all
cases. Other futures created by your thread might take priority. Consider the
case where you pushed a future onto the event loop which infinite loops before
you used await. Then your code will freeze.

------
zdragnar
I'm no functional fanatic, but one thing that I enjoy about it is the idea
that function calls- and futures- resolve to a value.

The code example where D doesn't get called last because of the missing await
grinds my gears something fierce, especially because I missed it the first
glance through. This type of bug is much harder to create when you don't
constantly rely on side effects everywhere.

That said, the article did a really good job if you're not already familiar
with the same concepts from JS land (with the event loop sans visible
microtasks, Promise instead of Future, abd web worker instead of Isolate).

~~~
Taig
This is my main point of criticism about Dart (and Flutter SDK design). While
Flutter is incredibly pleasant to work with compared to native Android or iOS
SDKs, it still feels so terribly ill-conceived in so many areas coming from a
functional background.

For the Dart part the Future-problems you already mentioned and the need for
async/await language level keywords is disappointing. Also if/else not being
an expression as well as syntactic complexity (e.g. defining constructors with
factory/static/const keyword combinations). Why come up with a new language in
2011 that repeats the null mistake?

Flutter gives you a beautiful and comprehensive set of widgets and makes a
great first impression. But when digging a bit deeper, things tend to get
ugly. Massive side effects hidden deep inside OOP structures (e.g. routing),
stuff like mixins, validation tied into view layer etc. I couldn't help but
find so many mistakes being repeated.

After spending a couple months with Flutter, I decided to ditch mobile
development all together. I wish they followed the direction react is steadily
going: promoting simple, pure widgets and passing values around, avoiding side
effects.

~~~
munificent
_> the need for async/await language level keywords is disappointing_

This is unfortunate [1], I agree. The challenge is that if you don't do that,
it's _very_ hard to compile the resulting code to efficient JavaScript. Dart
was initially only a web language and is now both a web and a mobile language.

You can think of function calls as having two different calling conventions:
return the value normally, or return it by invoking a continuation with the
result. Asynchronous functions require the latter. You could compile _all_
function calls to that style, but it's much slower and generates larger JS.

You could try to infer which functions should be called using an async style
and which shouldn't. That works in simple cases, but breaks down in complex
cases involving generics and higher-order functions. I don't know of any
literature that investigates how feasible this is in practice, so making your
language _rely_ on this optimization for tolerable performance is a very large
gamble.

This may change over time now that JS has generators and may be adding
async/await syntax, but Dart was designed before either of those existed.

 _> Also if/else not being an expression_

Yeah, everything being an expression is nice. I wish Dart had been designed
that way. The original designers of the language believed they needed to be
_very_ conservative in order to be successful and I think they overshot that
mark.

 _> syntactic complexity (e.g. defining constructors with factory/static/const
keyword combinations)_

Some of this is annoying, yes. It does have some practical value, though:

* The restriction around initializing final fields in the constructor initialization list, which is what forces you to use factory constructors in some cases, ensures a very nice property: it means in Dart you can _never_ observe a final field before it has been initialized. This is not true in C#, Java, or Kotlin. Now that we are working on non-nullable types for Dart [2], this will let us have sound non-nullable types, unlike those other languages.

* Personally, I find const to be more of a hassle than it's worth, but const constructors do let you define real compile-time constant objects of user-defined classes, which isn't something some other languages can express. That, in turn, lets you rely on very fast identity tests for checking equality between two objects. Flutter relies on this heavily when diffing the widget tree to determine which parts have changed efficiently.

 _> Why come up with a new language in 2011 that repeats the null mistake?_

Tell me about it [3]. (Note the date on that blog post.) We're working to fix
it now.

[1]: [http://journal.stuffwithstuff.com/2015/02/01/what-color-
is-y...](http://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-
function/)

[2]: [https://github.com/dart-
lang/language/issues/110](https://github.com/dart-lang/language/issues/110)

[3]: [http://journal.stuffwithstuff.com/2011/10/29/a-proposal-
for-...](http://journal.stuffwithstuff.com/2011/10/29/a-proposal-for-null-
safety-in-dart/)

------
vmsp
If you're interested in the Dart VM, I also recommend taking a look at
Vyacheslav Egorov's Introduction to Dart VM
([https://mrale.ph/dartvm/](https://mrale.ph/dartvm/)).

------
ridiculous_fish
What is the role of the `async` keyword in Dart? I couldn't understand it from
the article.

> the outcome of the method is a Future

Isn't the function signature sufficient to express this?

> it runs synchronously the code of that method up to the very first await
> keyword

How is this different from non-async methods?

> the next line of code will be run as soon as the Future, referenced by the
> await keyword, will have completed

But the await keyword is in the caller which is not statically known.

What is the difference between an async method and a method that returns a
Future?

~~~
krzat
It works like in every other language that supports async/await. Compiler will
convert code inside async method into a state machine to prevent it from
locking thread during await calls.

If you don't uses async you have to do compiler's job yourself.

~~~
ridiculous_fish
But why do we need a new keyword to express this? Isn't the return type
sufficient?

~~~
krzat
No, because you can write async functions without using async/await. Using
callback for example:

    
    
      Future<int> foo() {
          return Future.delayed(Duration(seconds: 1)).then((x) => 1);
      }
    

In this case there is no compiler magic needed. If you ask why async keyword
is needed if we can just infer it from await inside the method body - probably
only for readability.

~~~
ridiculous_fish
async is part of the method signature, the public facing part of the method.
But it controls an implementation detail, namely whether the function body may
use await (as you say asynchronous functions are not required to use await).

I found a link [1] that described async as a compatibility affordance in C# 5:
legacy code may use await as a variable name, so async opts into the
contextual await keyword. But Dart doesn't have such legacy, so the keyword
doesn't seem as well motivated.

[https://blogs.msdn.microsoft.com/ericlippert/2010/11/11/asyn...](https://blogs.msdn.microsoft.com/ericlippert/2010/11/11/asynchrony-
in-c-5-part-six-whither-async/)

------
huhtenberg
A typo:

    
    
        while (microTaskQueue.isNotEmpty){
            ...
            return;
        }
    

It should be either "if { ... return }" or just "while { ... }".

------
ymannn
Wow, I have no idea how similar Dart is to JavaScript.

Isolate == Worker Future == Promise

~~~
inquisitiveio
They touch on the importance of compiling to efficient JavaScript in their FAQ
[https://www.dartlang.org/faq#q-why-isnt-dart-more-like-
haske...](https://www.dartlang.org/faq#q-why-isnt-dart-more-like-haskell--
smalltalk--python--scala--other-language)

But it probably also helps that one of dart's creators, Lars Bak also
developed the V8 JavaScript engine.

------
kamura
For someone used to work with async/await only in C#, I was not expecting the
execution model to be so fundamentally different in Dart.

Something to try to wrap my head around later!

