Asynchronous Programming

Learn about utilizing asynchronous programming in Python.

With the constructions we've seen so far, we can create asynchronous programs in Python. This means that we can create programs that have many coroutines, schedule them to work in a particular order, and switch between them when they're suspended after a yield from has been called on each of them.

Parallelizing I/O operations

The main advantage that we can take from this is the possibility of parallelizing I/O operations in a nonblocking way. What we would need is a low-level generator (usually implemented by a third-party library) that knows how to handle the actual I/O while the coroutine is suspended. The idea is for the coroutine to effect suspension so that our program can handle another task in the meantime. The way the application would take control back is by means of the yield from statement, which will suspend and produce a value to the caller (as in the examples we saw previously when we used this syntax to alter the control flow of the program).

Get hands-on with 1400+ tech skills courses.