r/learnpython • • 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?

9 Upvotes

15 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Nov 07 '24

I’ve started using connection pools to the database for my applications. I used to open the connection, run the sql query, then close the connection. But I found the pool to run much smoother. That wouldn’t exactly help since you would have wanted to catch the flaw anyways. But figure I’d throw that out there.

1

u/Conscious-Ball8373 Nov 08 '24

SQLAlchemy does this all under the covers for me. I don't even think about connection pooling, it just happens. At some point it becomes necessary to tune the pool parameters but not for a long time.