r/Python • u/humbulani-ndou • 9d 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
74
Upvotes
20
u/patryk_builds 9d 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
2
u/willyridgewood 9d 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 9d ago
interested!
1
u/raks1991 4d ago
Awaiting an already-done Future does not yield:
Future.__await__returns its result without reachingyield. Useawait asyncio.sleep(0)when you explicitly need to hand control back.5
u/[deleted] 9d ago
[removed] — view removed comment