A StackOverflow participant asked me to recommend resources for learning about asyncio. While the official documentation provides a good reference manual, it doesn’t work as a tutorial – despite the authors’ best intentions, it is hard to follow unless one already understands the underpinnings. Fortunately there are quite a lot of introductory articles about asyncio. As with learning any novel concept, it takes some time to wrap one’s head around it, and one often needs to look at the same thing from many different angles before reaching that a-ha! moment that makes it all worth it. Here is a list of resources that I personally found enlightening.
- A guide to asynchronous programming in Python with asyncio – covers the basic concepts. Introduces coroutines, futures, tasks, and event loop with simple examples.
- Exploring Python 3’s Asyncio by Example – a more detailed introductory text. It shows spawning coroutines in parallel and using
aiohttpfor real-world tasks. It is written in the older
yield fromsyntax, but don’t be put off by that, just imagine
awaitin place of
yield from, as their semantics in asyncio is identical. (Also,
yield fromis still supported, so the code works in modern asyncio just fine.)
- AsyncIO for the Working Python Developer – covers the basic stuff, but also the
waitflags and timeouts, task cancellation, and exception handling.
- asyncio PyMOTW article – a very detailed reference for asyncio with examples for every covered concept. Includes examples of interaction between asyncio and synchronous code.
- How the heck does async/await work in Python 3.5? – an in-depth explanation of asyncio beginning with generators and moving on to
await. Ends with a simple event loop implementation.
- Python Concurrency From the Ground Up – a talk by the inimitable Dave Beazley. During the talk, Dave implements a full-featured event loop with support for coroutine dispatch, timeouts, and IO polling, typing and testing all of it in front of live audience. A friend likened this talk to the programming equivalent of jazz improvisation combined with stand-up comedy. Although the actual asyncio event loop implementation differs from the one presented there, this talk is a fantastic aid for understanding the dispatch of Python coroutines, and how it relates to the classic “event loop” concept. If you’re curious about coroutine-native event loops as presented in the talks, look up Dave’s curio and Nathaniel J. Smith’s trio libraries.