event loop

Making Dart asynchronous : hello event loop

If you work with Dart or even Javascript, you HAVE to know about the Event loop. This is what makes Dart or Javascript asynchronous. Both are single-threaded languages, this means they use only one thread and are in fact synchronous languages.

I can hear you saying “Wait, what ? Dart is single-threaded ?” In fact, Dart provides isolates, which are, in Dart’s word, some kind of multithreading. But not really…. We’ll talk about it later, let’s dive in the event loop concept first, assuming we excecute our Dart code in the main isolate, that is single-threaded.

As a good single-threaded language, Dart executes only one operation at a time. This means that as long it’s executing something, the next operations coming in the queue have to wait for it to end. So, when Dart is executing a lenthy task, it might possibly block the thread, and the app doesn’t respond to user interactions anymore until the lengthy task is performed. This could be quite annoying right ?

The Queues and the loop

In this single-threaded isolate, Dart provides 2 queues : one for the microtasks, another for the events. The event loop is a sort of infinite loop that runs all the time to watch at Dart’s microtasks and events queues. Both queues are FIFO (first-in, first-out), so the first thing to come inside the queue will be the first one to get out.

The event loop watches for microtasks in higher priority. If the microtasks queue is not empty, it execute the first microtask and the next until there is no more microtasks in the queue. Then, when the microtasks queue is empty, it watches at the event queue and execute the first event. After executing the first event, the event loop watches again in the microtasks queue, and if it’s empty, it processes another event, and watches first the microtasks, and events, and micr… ok you get it? This is an infinite loop watching the microtasks, and then, the events. And again.

Things you need to remember are :

  • Dart comes in an isolate, which provides a single thread.
  • This thread comes with its own piece of memory, a Dart runtime, two FIFO queues (one for microtasks, one for events) and useful stuff like an event loop or a powerful garbage collector.
  • Event loop watches the queues all the runtime long.
  • Microtasks have higher priority than events.

This is kinda hard to explain the event loop with written words, but I found a great video that shows this process very clearly, and helps a lot to understand what’s happening here :

I hope this makes Event loop easier to understand for you, as it’s a really important concept for your application performances.

This talk of Philip Roberts is one of the best I’ve seen about this concept, this is a Javascript talk but the event loop is the same, so don’t be shy and take a look :

In the next post, I’ll talk about the Dart isolates. Stay tuned !

Elya

Hi ! I'm a junior web developper and UX designer. Living in France, mother of 2, bookworm, cats lover, JS girl, yada yada yada. The usual stuff. Let's talk about code !

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

back to top