r/Python • u/humbulani-ndou • 8d 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
2
u/willyridgewood 8d 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
1
u/raks1991 3d ago
Awaiting an already-done Future does not yield: Future.__await__ returns its result without reaching yield. Use await asyncio.sleep(0) when you explicitly need to hand control back.
5
8d ago
[removed] — view removed comment
1
u/humbulani-ndou 8d ago
I intentionally set-up Asyncio to use Python and avoid C. The purpose is more educational and demonstration of concepts.
20
u/patryk_builds 8d 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