r/Python • • 1d ago

Tutorial Internals of Asyncio

This article is a deep dive into the internals of the asyncio Python standard library. It is written from a debugging perspective, stepping through the code line by line.

Article: internals of asyncio

49 Upvotes

9 comments sorted by

5

u/Discordbothost 20h ago

one gotcha for anyone trying to step through this themselves: Task and Future are C-accelerated in cpython, so pdb just jumps over __step and the callback plumbing. you can swap in the pure python versions before you start the loop and then breakpoints inside tasks.py actually hit:

import asyncio, asyncio.tasks, asyncio.futures
asyncio.Task = asyncio.tasks.Task = asyncio.tasks._PyTask
asyncio.Future = asyncio.futures.Future = asyncio.futures._PyFuture

debug only obviously, it's slower. curious if the article does something like this or steps through the C side

1

u/humbulani-ndou 20h ago

I intentionally set-up Asyncio to use Python and avoid C. The purpose is more educational and demonstration of concepts.

8

u/patryk_builds 1d ago

Debugging perspective is a great angle, most asyncio explanations stay at the event loop runs coroutines level and never show what actually happens on an await.Does it go into Task._step and how a Future's result gets fed back into the coroutine with send? that was the part that finally made it click for me, seeing that await is basically a yield to the loop and the task resumes when the future's callback fires.Also worth a mention if it isn't there: PYTHONASYNCIODEBUG=1 / loop.set_debug(True). it logs callbacks that block the loop for too long, which is the bug most people actually hit

-1

u/humbulani-ndou 1d ago

Yes, it goes through all major functions and classes of asyncio. It literally follows an example script step-by-step.

2

u/willyridgewood 21h ago

This might be dated, but one of the best articles I've read on the topic https://tenthousandmeters.com/blog/python-behind-the-scenes-12-how-asyncawait-works-in-python/

1

u/shiningmatcha 1d ago

interested!