|Python Adds Async And Await|
|Written by Alex Armstrong|
|Thursday, 07 May 2015|
It looks as if the C# inspired async and await approach to asynchronous programming is eating the world. Python 3.5 is the latest language to adopt it.
The latest Python Enhancement Proposal (PEP) to be accepted is:
PEP 0492 -- Coroutines with async and await syntax
Authored by CPython core developer, Yury Selivanov who is founder of Sprymix.com, it adds the async and await way of writing asynchronous code to the next version of Python. Notice that this isn't going to be back ported to any branch of Python 2 because its development is at an end. This could be a really good reason for upgrading to Python 3, although this said there are already a lot of new things planned for Python 3 that makes it worth forgetting Python 2.
However, if the load is slow and is being done on the UI thread, waiting would make the entire application freeze. The direct solution is to convert the processor data step into a function and pass it to the load data as a callback. This works but gets very complicated very quickly leading to so called callback hell.
The async and await solution solves the problem without changing the natural flow of control. You can await any function call that has been declared async. So if load data was declared "async load data" then you could write your program as:
The UI thread would be freed as soon as the await load data was executed and it would return to process data when the load was complete. Essentially you are asking the compiler/interpreter to do the job of creating a callback equivalent to process data but also to save and restore the entire context. For example if the load and process were in a loop then the await would restore the state of the loop when it restarted.
Put simply await and async make asynchronous programming just the same as synchronous programming by having the compiler do the work of converting the synchronous algorithm into an asynchronous one.
The Python implement of async and await also includes native coroutines as a new standalone concept. The async keyword is used to define a native coroutine and the await keyword is used to yield control in place of the yeild keyword. For example:
The async marks the get_data as a coroutine. The await calls slowdatadownload and releases the thread of execution which only returns when slowdatadownload completes.
For this all to work, slowdatadownload has to be an awaitable, which can be a native coroutine, a generator based coroutine or an object with an _await_ method.
There are other facilities being introduced, like an async for; and there are some points of interrelation with generators, which are used to implement native coroutines.
The list of languages that support or are going to support async and await is growing - C#. Hack/HHVM, Dart, Scala and there is a proposal to add them to ECMAScript and C++.
This is becoming the standard way to do async and this is not a bad thing.
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
or email your comment to: firstname.lastname@example.org
|Last Updated ( Thursday, 07 May 2015 )|