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.
Asynchronous operation is a fact of life for languages that work with single-threaded user interfaces like C# and JavaScript, but Python isn't particularly tied to UI programming. The problem with asynchronous programming is that it spoils the simple flow of control that you would have in a synchronous program. For example, if you want to load some data over the internet and then process it you would write:
load dataprocess data
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:
await load dataprocess data
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.
This is much superior to other approaches and if you think that JavaScript's promises or futures are a good idea think again - this is much easier and it will be in the next version of JavaScript.
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:
async def get_data(): data=await slowdatadownload() ...
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.
PEP 0492 -- Coroutines with async and await syntax
Dart 1.9 Now With Async And Await
Facebook Introduces Hack, A Better PHP
Xamarin Brings Async To Mono
What Is Asynchronous Programming?
Task.js Asynchronous Tasks In JavaScript
Async, Await and the UI problem
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.
NativeScript 6.0 has been released with more support for Webpack and faster delivery of patches.