Python 3.5 will Support Async/Await Asynchronous Programming

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.

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:

InfoQ Weekly Newsletter

Join a community of over 250 K senior developers by signing up for our newsletter. If you are based in the EEA, please contact us so we can provide you with the protections afforded to you under EEA protection laws.

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

Email Address

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

Company name:

Keep current company name

Update Company name to:

Company role:

Keep current company role

Update company role to:

Company size:

Keep current company Size

Update company size to:

Country/Zone:

Keep current country/zone

Update country/zone to:

State/Province/Region:

Keep current state/province/region

Update state/province/region to:

Subscribe to our newsletter?

Subscribe to our architect newsletter?

Subscribe to our industry email notices?

By subscribing to this email, we may send you content based on your previous topic interests. See our privacy notice for details.

You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.

We notice you're using an ad blocker

We understand why you use ad blockers. However to keep InfoQ free we need your support. InfoQ will not provide your data to third parties without individual opt-in consent. We only work with advertisers relevant to our readers. Please consider whitelisting us.