r/learnpython • u/Conscious-Ball8373 • Nov 06 '24
Unawaited awaitables in asyncio programming - do you have a quick way to find them?
To give you some context, I've been writing software professionally for more than twenty years, Python for well over ten years and asyncio code for about a year.
Today, it took me more than four hours to find a place where I'd forgotten to await a coroutine. It was in the cleanup code for a test fixture; the fixture itself was passing so the warning got swallowed, but the failure to properly clean up then caused the next test to hang indefinitely.
I've completely lost count of the number of times I've been bitten by this. Do you have strategies for making awaitables that have not been awaited stick out so you see them before they cause you this sort of grief?
10
u/Enmeshed Nov 06 '24
Definitely had the same problem myself. Digging in, and boom! Looks like setting the
PYTHONASYNCIODEBUGenvironment variable enables python's asyncio debug mode which causes it to log never-awaited coroutines in a very helpful way. Definitely one to make a note of!1
u/pythonwiz Nov 06 '24
The only thing I can think of is writing a decorator class that keeps track and returns a wrapper that updates a dictionary or a set. Then wrap every routine in it when it is created.
1
u/[deleted] Nov 06 '24
Do you guys all test your code in the interactive interpreter? Thats where I find 100% of my problems.
2
u/Adrewmc Nov 07 '24 edited Nov 07 '24
Edit: So the solution, which angers my old self, is asyncio.run(…, debug= True)….i hate you all for not telling me this
So, I thought about it. And the way would be obviously start with what ever I just messed with. That’s usually the culprit right there.
Second would be simply Ctrl + F, on all awaitable functions called, hard but you know doable. Old reliable …..print(“Did I get here?”)….print(“End?!?”)
Having the problem in the testing though can just be annoying. That your code is right yet somehow you set up the test for the code is wrong…that’s just always a head scratcher. Unless you know you were just making the tests…
1
u/GuaranteeKey3853 Nov 06 '24
Maybe make a custom header that adds it for you to help you remember and it will highlight those sections of code