What is a Coroutine Anyway?
By John Reese

AsyncIO uses coroutines to deliver high performance from a single thread. But coroutines can be mysterious. How do they work? Starting from first principles, we’ll take a look at the basic concepts of coroutines and the unique problems they solve, then finish by deconstructing the core pieces of the AsyncIO framework. This talk is for developers of all backgrounds. No CS degree required!

This talk will start with basic concepts of functions, including how they are represented in memory, how state is tracked, and how function calls interact with the stack. We’ll then cover the common methods of running multiple functions concurrently, as well as the benefits and difficulties of concurrency in Python.

We’ll then introduce the concept of coroutines, a variant of functions, and discuss how coroutines manage state and execution differently from functions. We’ll show some high level examples of coroutines that communicate with each other, and look at how they can be of use for I/O bound workloads.

Then we’ll finish by showing how coroutines are implemented in Python, what the async/await keywords are actually doing when you use them in your code, and how all of these concepts are leveraged by the AsyncIO framework to build high performance applications in modern, clean Python.

John Reese

John is a Production Engineer at Facebook, working on the internal Python Foundation team to build both open source and internal tools that make Python better for Facebook engineers and the wider Python community. They created the open source automated refactoring tool Bowler, as well as a number of AsyncIO libraries, including aiosqlite and aiomultiprocess. They have a wide variety of prior experience, including exabyte scale photo and video storage, network security, telecommunications, and video games.