> On Jul 30, 2016, at 3:32 AM, Герасимов Михаил <gerasimov-...@yandex.ru> wrote:
> 
> When asynchronous generator is about to be garbage collected event loop 
> starts task to execute aclose, right? Can't such situation happen when this 
> task is not finished at the moment event loop is closed? Something like:


Yes, such situations can happen.  BUT they can happen for regular coroutines 
too!  Try running the following:

async def coro1():
 try:
     print('try')
     await asyncio.sleep(1)
 finally:
     await asyncio.sleep(0)
     print('finally')
async def coro2():
 await asyncio.sleep(0)
loop = asyncio.get_event_loop()
loop.create_task(coro1())
loop.run_until_complete(coro2())
loop.close()

In the above script, coro1() enters its `try` block and prints “try”; loop ends 
the execution because coro2() is completed; and coro1() will never execute its 
`finally` block.

Long story short - you have to be extra careful when you’re closing the event 
loop.  I believe that not executing “finally” statements when the process is 
about to complete will be more common for coroutines than async generators.

Also, in real life programs you don’t run and close event loops more than once 
per process lifetime.  So it should be fine if you have a few non-exhausted 
generators and coroutines being GCed without proper finalization just before 
the process exits.  And, BTW, Python interpreter will issue a ResourceWarning 
that it cannot finalize coroutines and/or async generators.


Yury
_______________________________________________
Async-sig mailing list
Async-sig@python.org
https://mail.python.org/mailman/listinfo/async-sig
Code of Conduct: https://www.python.org/psf/codeofconduct/

Reply via email to