Now when I started to think about this post I was worried it was going to be rather long and arduous to research (although I believe class is going to

Unravelling `async` and `await`

submited by
Style Pass
2021-05-25 01:00:09

Now when I started to think about this post I was worried it was going to be rather long and arduous to research (although I believe class is going to ultimately win that crown šŸ˜œ), but then I remembered I had written a blog post about how async and await worked in Python 3.5. As part of that post I went through the history of how Python got to that point of asynchronous programming, which meant I had already dived into the details of how Python evolved earlier syntax to what it has today! Thus this post is going to draw heavily from my async history post to save myself some time, so if you feel I skimmed over details then chances are it's because I covered it in my other post.

Let's go from the outside in, tackling async first. It turns out that unravelling async def is very straightforward thanks to the types.coroutine decorator which is what async def evolved from. This decorator sets a flag on the code object for the generator to distinguish it from any other plain generator. Otherwise that's it as async functions are fundamentally generators.

The await expression evolved from yield from, but with two key tweaks: checking that the object is awaitable, and also supporting awaitable objects which define __await__().

Leave a Comment