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