BT

InfoQ Homepage News Python 3.5 will Support Async/Await Asynchronous Programming

Python 3.5 will Support Async/Await Asynchronous Programming

Bookmarks

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 ...')

Rate this Article

Adoption
Style

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.

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

Community comments

  • Nice, but will take years for PyPy to catch up!

    by Nikolay Kolev /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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

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

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.