Brussels / 2 & 3 February 2019


Demystifying Coroutines and Asynchronous Programming in Python

In this talk we will explore how coroutines work and how they are implemented in Python. We will learn that generators lie at the heart of coroutines, and how this long-standing feature of Python evolved over time to support the new capabilities of asynchronous programming.

It’s a time travelling tale that explores the milestones in Python that relate to generators, coroutines, and asynchronous programming.

The talk is intended to explore coroutines and asynchronous programming in Python, in full detail. It provides a historical revision of all the milestones in Python that led to the current implementation of coroutines, asynchronous programming, and event loops (asyncio) as we know them today. Starting from generators, it reviews their original idea and rationale, and then it explores how they became the pythonic solution for supporting coroutines, by extending their interface with new methods (PEP-342). Later on, generators (and coroutines) were extended once again in PEP-380, to support even more advanced functionality (like the "yield from" syntax). This was a major milestone that actually allowed programmers to start working with non-blocking I/O asynchronously fashion, and the idioms used for that resemble modern asynchronous programming with Python. Understanding what's behind those idioms helps to explain much of the current asynchronous frameworks.

The dynamic nature of Python made the syntax prone to mixing iterators with coroutines, so the community (wisely) agreed on the fact that the language had to evolve, and new syntax needed to be added. New specific types were added for coroutines in Python 3.5, and Python 3.6 incorporated asynchronous generators. Finally in Python 3.7, "async" and "await" were promoted to keywords, and there were several enhancements to the asyncio library, marking the current state of the art of asynchronous programming in Python.

The talk concludes by providing food for thought on what asynchronous programming actually means in Python, and what to expect for the future (Python 3.8 and beyond).


Mariano Anaya