Asyncio: Am I doing it wrong?

I have been using asyncio for the core part of my job for one year and a half already, and I still feel it’s incredibly difficult to get right. Trivial things work trivially, but when I want to handle graceful cancellation of tasks and exception handling, everything falls apart. Although I have read Lynn Root superb tutorial “asyncio: We did it wrong” (hence the title of my post) a dozen times, asyncio still makes my head hurt. Yesterday I lost the full day debugging a task getting stuck in aio-pika, and I am on the verge of giving up. PYTHONASYNCIODEBUG=1 python -Werror ... only helps scratching the surface.

Should I just give up on asyncio, and embrace trio? That would be unfortunate, because many async libraries only support asyncio. anyio doesn’t help if my dependencies don’t use it.

If not, what are in your opinion the absolute best resources to learn asyncio “in production”, and what tips and tricks do you use to debug things like dangling tasks, non retrieved exceptions and so forth? In particular, knowing “where the program is stuck” would be super helpful.

2 Likes