Debugging asyncio
The asyncio
module has a few special provisions to make debugging somewhat easier. Given the asynchronous nature of functions within asyncio
, this is a very welcome feat. While the debugging of multithreaded/multiprocessing functions or classes can be difficult – since concurrent classes can easily change environment variables in parallel – with asyncio
, it’s just as difficult, if not more, because asyncio
background tasks run in the stack of the event loop, not your own stack.
If you wish to skip this part of the chapter, I urge you to at least read the section on Exiting before all tasks are done. That covers a huge pitfall with asyncio
.
The first and most obvious way of debugging asyncio
is to use the event loop debug mode. We have several options for enabling the debug mode:
- Set the
PYTHONASYNCIODEBUG
environment variable toTrue
- Enable the Python development mode using the
PYTHONDEVMODE
environment...