I tried to implement asyncronious generators based on asyncio recently, you can see result here:

https://github.com/germn/aiogen

I also faced problem with cleanup.

First thing is where to call `.close` (or async `.aclose` for AG since it raises exception inside async function). While regular generator calls it inside `__del__` as I understand there's no guarantee AG's event loop wouldn't be closed at this moment. I think AG can be closed at the moment it's parent task done. Since this is non-async callback we can start task to call `.aclose`:

https://github.com/germn/aiogen/blob/master/aiogen/agenerator.py#L35

Nothing awaits for cleanup task so we should sure that is would be finished before event loop is closed. I found nothings better than to decorate event loop's `.close` method:

https://github.com/germn/aiogen/blob/master/aiogen/agenerator.py#L52

That all doesn't look like ideal solution, but it works.
_______________________________________________
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