AsyncIO
Learn about the AsyncIO Python concurrency with efficient, readable async applications through futures, event loops, and coroutines.
AsyncIO is the current state of the art in Python concurrent programming. It combines the concept of futures and an event loop with coroutines. The result is about as elegant and easy to understand as it is possible to get when writing responsive applications that don’t seem to waste time waiting for input.
Coroutines and event loop
For the purposes of working with Python’s async
features, a coroutine is a function that is waiting for an event, and also can provide events to other coroutines. In Python, we implement coroutines using async def
. A function with async
must work in the context of an event loop which switches control among the coroutines waiting for events. We’ll see a few Python constructs using await
expressions to show where the event loop can switch to another async
function.
Cooperative multitasking
It’s crucial to recognize that async
operations are interleaved, and not—generally—parallel. At most one coroutine is in control and processing, and all the others are waiting for an event. The idea of interleaving is described as cooperative multitasking: an application can be processing data while also waiting for the next request message to arrive. As data becomes available, the event loop can transfer control to one of the waiting coroutines.
AsyncIO has a bias toward network I/O. Most networking applications, especially on the server side, spend a lot of time waiting for data to come in from the network. AsyncIO can be more efficient than handling each client in a separate thread; then some threads can be working while others are waiting. The problem is the threads use up memory and other resources. AsyncIO uses coroutines to interleave processing cycles when the data becomes available. ...