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. 

python3

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 data
process 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 data
process 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.

python3

 

Banner


Random Gifts For Programmers
24/11/2024

Not really random. Not even pseudo random, more stuff that caught my attention and that I, for one, would like to be given. And, yes, if I'm not given them, I'd probably buy some for myself.



RAG from Scratch
10/12/2024

The "RAG from Scratch" tutorial by Langchain coupled with the "RAG playground" are two great educational resources that will help you kickstart your journey with RAG.


More News

 

espbook

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Thursday, 07 May 2015 )