Python 3.5 with PEP 492 introduces special syntax for coroutines and awaitables and illustrates the await
keyword as follows:
The following new await expression is used to obtain a result of coroutine execution:
async def read_data(db): data = await db.fetch('SELECT ...') ...
await
, similarly toyield from
, suspends execution ofread_data
coroutine untildb.fetch
awaitable completes and returns the result data.
It continues to define that awaitables can be coroutines or Future-like objects, with a specific definition of Future-like objects.
But despite reading the PEP, I don't quite understand what's special about await
. In the line quoted from the PEP above, who returns the result data — await
or read_data
(or both)? Surely no matter how I call db.fetch('SELECT ...')
(directly, with yield from
, or with await
), read_data
will not continue until it gets something (possibly None
) from db.fetch
? Then what's special about await
and how is it related to async
?
Clearly I'm missing something.