Say I have the following function
async def f1():
async for item in asynciterator():
return
What happens to the async iterator after
await f1()
? Should I worry about cleaning up or will the generator be somehow garbage collected when it goes out of sight?
TL;DR Python's gc and asyncio will ensure eventual cleanup of incompletely iterated async generators.
"Cleanup" here refers to running the code specified by a
finally
around theyield
, or by the__aexit__
part of the context manager used in awith
statement around theyield
. For example, theprint
in this simple generator is invoked by the same mechanism used by aaiohttp.ClientSession
to close its resources:If you run a coroutine that iterates through the whole generator, the cleanup will be executed immediately:
Note how the cleanup is executed immediately after the loop, even though the generator was still in scope without the chance to get garbage collected. This is because the
async for
loop ensures the async generator cleanup on loop exhaustion.The question is what happens when the loop is not exhausted:
Here
gen
got out of scope, but the cleanup simply didn't occur. If you tried this with an ordinary generator, the cleanup would get called by the reference countered immediately (though still after the exit fromtest
, because that's when the running generator is no longer referred to), this being possible becausegen
does not participate in a cycle:With
my_gen
being an asynchronous generator, its cleanup is asynchronous as well. This means it can't just be executed by the garbage collector, it needs to be run by an event loop. To make this possible, asyncio registers the asyncgen finalizer hook, but it never gets a chance to execute because we're usingrun_until_complete
which stops the loop immediately after executing a coroutine.If we tried to spin the same event loop some more, we'd see the cleanup executed:
In a normal asyncio application this does not lead to problems because the event loop typically runs as long as the application. If there is no event loop to clean up the async generators, it likely means the process is exiting anyway.