A Beginners Guide to Dart - Concurrency, Asynchronous Evaluation and Parallelism in Dart - Part Eight

in #utopian-io5 years ago (edited)

dart-bird.jpg

Repository

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

What Will I Learn?

  • You will learn about Concurrency in Dart
  • You will learn about Parallel programming in Dart
  • You will learn how to use Futures to perform Asynchronous Evaluations in Dart
  • You will learn about Completers
  • You will learn how to Spawn an Isolate
  • You will learn how to use Ports to Send and Receive Messages

Requirements

System Requirements:
OS Support for Dart:
  • Windows
  • macOS
  • Linux

Required Knowledge

  • The Dart SDK
  • A Dart supported text editor (Dart Pad can be used)
  • A little time to sit and watch a video and some patience to learn the language

Resources for Dart:

Sources:

Dart Logo (Google): https://www.dartlang.org/

Difficulty

  • Beginner

Description

In this Dart Video Tutorial, we take a look at how we can preform Concurrency and Parallel programming in Dart. We take a look at Futures and Completers. We also look at Isolates and their Ports and Messages. We talk about why Asynchronous Evaluation is necessary and powerful and we talk about the difference between Parallel programming and Asynchronous programming.

Using Futures and Completers to Preform Asynchronous Tasks

Often when dealing with systems that are outside of the control of your current program; you have to deal with task that may take unspecified amounts of time to complete. If you run these tasks in a synchronous manner, they block the code that follows the execution of the task. This becomes a problem especially when dealing with computations that might fail or might take very long periods of time. To overcome this problem, Dart exposes a Future API that uses Completer Objects to define when the task has completed. These Futures allow Dart to specify Events which are pushed into an external Queue. When the Completer object comes back with a completion, the Future then wraps the result of this completion and serves it where it is needed.


thread-dart.png

The above Graph highlights Dart's main execution loop. Dart is a single threaded programming language and when a program starts it checks two main queues. It first checks a microtask queue to see if there are any scheduled microtasks. If there are, then it will execute those microtasks and loop back to re-check the queue. If there are none, then the execution will move to the Event queue. This Event queue is where Futures get logged and queued for execution. Dart executes the Event and then moves back to check to see if there are any Microtasks and Events in the Queues before finishing its execution once the Queues are empty.

Creating Parallel code with Dart using Isolates

Dart is inherently a single threaded programming language. The main execution loop allows the program to work with asynchronous code but another thread is needed to create parallel code. This is where the concept of the Isolate comes into play. Each isolate is another instance of the same execution loop that was highlighted above. These instances do not share memory between one another and can only communicate using messages. These messages are passed through unidirectional Ports which means that an Isolate can only either send data to another Isolate or receive data from another Isolate.


isolate-graph.png

Above is a graph that showcases the general concept behind isolates. Isolate One which is the main isolate spawns isolate two in this case and it does so at the very beginning of execution. Both isolates have identical execution lifecycles with different microtasks and events depending on the tasks that have been assigned to them by the developers. Both have their own Event and Microtask Queues which allows these isolates to deal with Asynchronous code. Isolate Two sends messages to Isolate One and it does this through a Port. Inside of Isolate Two a Send Port Object and inside of Isolate One is a Receive Port Object. These two objects facilitate the message passing between these two Isolates.

The Source Code for this video may be found here: https://github.com/tensor-programming/dart_for_beginners/tree/tensor-programming-patch-7

Video Tutorial

Curriculum

Projects

Sort:  

Hi @tensor,

Thank you for another great turtorial on Dart.

Your flow chart graphs are very effective in showing how the programming works. This is a great help to newbies who are starting to learn.

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? Chat with us on Discord.

[utopian-moderator]

Thanks @rosatravels; I figured that would be a better way to show off the concepts compared to just showing code.

Thank you for your review, @rosatravels! Keep up the good work!





This post has been voted on by the SteemSTEM curation team and voting trail in collaboration with @curie.

If you appreciate the work we are doing then consider voting both projects for witness by selecting stem.witness and curie!

For additional information please join us on the SteemSTEM discord and to get to know the rest of the community!

Hi @tensor!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @tensor!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

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

Vote for Utopian Witness!

Hi, @tensor!

You just got a 6.45% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.

Congratulations @tensor! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 16000 upvotes. Your next target is to reach 17000 upvotes.

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

3 years on Steem - The distribution of commemorative badges has begun!
Happy Birthday! The Steem blockchain is running for 3 years.
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.028
BTC 54483.11
ETH 2297.01
USDT 1.00
SBD 2.28