BT

Python 3.5 will Support Async/Await Asynchronous Programming

by Sergio De Simone on May 07, 2015 |

Python 3.5 will add support for coroutines with async and await syntax, according to Python Enhancement Proposal (PEP) #0492. The proposal aims at making coroutines a native Python language feature and to "establish a common, easily approachable, mental model of asynchronous programming."

The new proposed syntax to declare a coroutine is the following:

async def read_data(db):
    pass

Specifically, async is the keyword that makes a function to behave as a coroutine, even if it does not contain await expressions. Such a function will return a coroutine object when executed.

Inside of a coroutine body, the await keyword can be used to make an expression suspend the execution of the coroutine and wait for some processing to complete:

async def read_data(db):
    data = await db.fetch('SELECT ...')
    ...

A form of coroutines has long been available in Python thanks to enhanced generators, i.e. Python generators that are treated as coroutines when the yield or yield from statements appear in their body.

This is an example of how a generator-based coroutine can be used:

>>> def createGenerator():
...    mylist = range(3)
...    for i in mylist:
...        yield i*i
...
>>> mygenerator = createGenerator()
>>> for i in mygenerator:
...     print(i)
0
1
4

In the code above, each time the generator is called in the for loop, a new value from the generator for loop is returned.

More examples of await use can be found in the mentioned PEP #0492.

The new proposal for coroutines has the goal to clearly separate generators from coroutines, with the following expected benefits:

  • make it easier for new developers to handle the two concepts, since they would not share the same syntax;
  • remove the cause for "unobvious errors" due to the yield statement being inadvertently removed from a coroutine while refactoring, thus making the coroutine to be treated as a generator.

The async/await syntax allows developers to write code as if it were sequential, but the compiler will implement it through a sequence of coroutines, thus making it effectively concurrent. Going back to the previous example, async/await makes it possible to write multiple await statements sequentially, as if each statement would block and wait for the result to be available, but this would not cause any actual block:

async def read_data(db):
    data = await db.fetch('SELECT ...')
    if (data...)
        await api.send(data ...')

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Nice, but will take years for PyPy to catch up! by Nikolay Kolev

Given PyPy3 supports just 3.2.5 today, I can imagine taking them at 2 years after 3.5 release to catch up.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

1 Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
Marketing
InfoQ.com and all content copyright © 2006-2015 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT