Is async keyword an ultimate replacement for asyncio module?
https://softwareengineering.stackexchange.com/questions/289903
Question
Recently, there was a rise of interest toward asynchronous programming in Python community. First prominent expression of this interest was the appearance of Asyncio —codenamed Tulip— framework [PEP 3156] in the standard library, and then, after a couple of years, the capability to write async code was introduced to the core of the language through async def
construction [PEP 0492].
Some core developers who participated in designing Asyncio claim that the latter will actually benefit from async
keyword introduction [Andrew Svetlov on Google Plus]. From my perspective, though, it seems like async
brings pretty much the same functionality as Asyncio does. Perhaps it's just me missing something.
So, the question is, shall async
take over Asyncio, or they are really that much different?
Was Asyncio just a playground before async
has been approved?
Otherwise, how can Asyncio leverage the new async
powers of the language?
Solution
asyncio
is a set of libraries. The PEP and implementation define event loop abstractions, protocols for tasks to interact with the event loop, and various I/O classes and concurrency primitives that can be used in asynchronous code. The way to write these tasks is/was to write a generator and the way to wait on the completion of another task is/was to yield from that_task
.
aync
extends the language with a different way to write generators (here called coroutines to avoid confusion) that is similar but more powerful and useful for writing asynchronous tasks with asyncio
(and other, similar event loops). await
is like yield from
conceptually and in practice. The async
PEP does not define any APIs (except the bare bones to support the new built-in type), and does not affect the asyncio
API at all. It's simply a better way to use asyncio
.