The Fundamentals of Zones, Microtasks and Event Loops in the Dart Programming Language - Dart Tutorial Part 3

in #utopian-io7 years ago

dart-logo.png

Repository

https://github.com/dart-lang/sdk

What Will I Learn?

  • You will learn what a Zone is in Dart
  • You will learn how Microtasks work
  • You will learn about the Event loop
  • You will learn about the Event Queue and Microtask Queue
  • You will learn how Futures interact with Zones and the Event Loop

Requirements

System Requirements:
OS Support for Dart:
  • Windows 7 SP1 or later (64-bit)
  • macOS (64-bit)
  • Linux (64-bit)

Required Knowledge

  • Some knowledge of basic programming
  • Some understanding asynchronous execution
  • A fair understanding of development and Imperative or Object Oriented Programming

Resources for Flutter and this Project:

Sources:

Dart Logo: https://www.dartlang.org/

Difficulty

  • Intermediate

Description

Outline and Overview

In this Dart Video Tutorial, we take a look at some of the building block concepts of Dart in the form of Zones and the Event Loop. We look at Microtasks and how they interact with Zones. We also look at how futures work in relation to Zones and the Event Loop. We talk about how there are two queues; the Event Queue and the Microtask Queue and we talk about the implications of this on the Dart language.

Planning Events on a Single Thread

Dart is a single threaded programming language. Because of this, the language makes use of an event loop to schedule the execution of pieces of code. The event loop takes an item from the event queue and handles it. It repeats these steps over and over again until there are no more items in the queue. Generally, these events represent user input or output, timers and other side effects.

event_loop.png

In the chart above, we can see how the Task queue and microtask queues work together. The microtask queue is created the first time a microtask is scheduled and it only exists for as long as a microtask exists. The event queue or task queue exists from the start of the program to the end of execution. Futures, timers and other asynchronous processes move through this portion of the program.

Task order and its implications.

Once a program starts to execute in dart, the main function is executed. The program, checks to see if there is a microtask scheduled at any point during the execution of the application. If this comes back as true, then those microtasks are scheduled in the microtask loop and then executed after all of the top level code is executed. There is a check for Events/Tasks next and this then forces these events into the event loop. These events have the final order priority in line.

out_order.png

This image shows the order of an application that uses all three different execution types. First, we see the normal top level execution functions which gives us Top of function and Bottom of Main Function. The callbacks for the Microtasks are executed next with first microtask and Second microtask being the output for these two functions. Finally, we see the events execute through the two futures. Future Two executes first because Future one is a delayed future.

Zoning out a Single Thread.

On a very low level, dart uses Zones do decide execution order. Zones are the Asynchronous Dynamic Extent of a call. Zones are similar to NodeJs' domains or Java's thread-local storage. It is the computation that is preformed during execution and the asynchronous callbacks that are registered by the code. The compiler decides at which point to create a new zone based on the size of a task. We can also manually deal with zones directly which allows us to protect our applications from errors and override various basic features.

zoned.png

In this code block, we use the runZoned function to create a new zone. In this zone, we have a stream feeding numbers to a print function. If the print function hits the number 9 then an error is automatically thrown out. Because this is happening in the child zone of the root zone, the application is allowed to continue executed even after this error happens. Also, we are able to overload the print functionality of the zone which allows us to attach a timestamp to the front of each of the print statements in the zone.

The source code for this project can be found here

Video Tutorial

Curriculum

Proof of Work Done

https://github.com/tensor-programming

Sort:  

wow i love this! i just found this out thanks to @utopian-io leader @elear who gave you a shoutout in @ned s new "Steemit's hiring" post and hinted that youre a great developer and I remember youre name from seeing you on steemspeak.com discord @fyrstikken 's voice chat and i just am glad to have someone like you doing Programming lessons relevant to steem developers! Thanks for this, i upvoted and resteemed!

Thank you, though admittedly, I am not sure how many steem devs actually use Flutter or Dart or even Rust. If a few of you guys find it useful then I am happy though.

great videos
keep on the good work.
could you sometime make some tutorials with flutter & graphql
found some packages on the pub that I'd like to understand better

There are some plans to work with a dart built api and a flutter frontend. I may use GraphQL at some point as well, though RPC seems to be more useful atm.

Great work @tensor.

Something that stuck out to me was that you asked learners to consider how they think the software should act before they run it. This meta-cognition/analytical thinking is an excellent step in the learning process. We find this in the highest levels of Bloom's Taxonomy. We often overlook these creative and self analyzing tasks in learning for the more rudimentary memorization.

Great work.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you @buckydurddle. Because of how abstract the concepts covered in this video were, I really wanted to make sure to try to get the audience to connect with the material. I figured that asking the audience to consider the execution order would be a good way of doing so.

Agreed. Encouraging learner to engage in this level of thinking can really cement the concepts. Great work.

My motto:

Listen, smile, agree, and then do whatever you were gonna do anyway.

Not really sure what that means in this context.

Wow this is nice it's an eye opener thanks

Hey @tensor
Thanks for contributing on Utopian.
Congratulations! Your contribution was Staff Picked to receive a maximum vote for the video-tutorials category on Utopian for being of significant value to the project and the open source community.

We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Congratulations @tensor! You have received a personal award!

1 Year on Steemit
Click on the badge to view your Board of Honor.

Do not miss the last post from @steemitboard!


Participate in the SteemitBoard World Cup Contest!
Collect World Cup badges and win free SBD
Support the Gold Sponsors of the contest: @good-karma and @lukestokes


Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Coin Marketplace

STEEM 0.28
TRX 0.26
JST 0.039
BTC 94700.83
ETH 3385.50
USDT 1.00
SBD 3.38