One of the design decisions about `loop.close()` is that it doesn't do a single event loop iteration, making its behaviour highly predictable. To make `loop.close()` to run `loop.shutdown_asyncgens()` (which is a coroutine), we would have needed to change that.
One of the ways we want to mitigate this problem in Python 3.7 is to add a new function to bootstrap asyncio and run top-level coroutines: `asyncio.run()`. You can read more about it here: [1]. I'm working on a new PEP that will summarize asyncio changes in 3.7. I don't have a concrete ETA for it, but I'll try to get the first draft out by mid September. [1] https://github.com/python/asyncio/pull/465 Thanks, Yury On Jul 27, 2017, 11:24 PM -0400, Chris Jerdonek <[email protected]>, wrote: > I have a question about PEP 525 (Asynchronous Generators) which I'm > sure has a simple answer, but I didn't see it in the PEP or final > discussion: > https://mail.python.org/pipermail/python-dev/2016-September/146265.html > > Basically, why is the API such that loop.shutdown_asyncgens() must be > called manually? For example, why can't it be called automatically as > part of close(), which seems like it would be a friendlier API and > more helpful to the common case? > > I was trying asynchronous iterators in my code and getting the following > error: > > Exception ignored in: <generator object Queue.get at 0x7f950a667678 > Traceback (most recent call last): > File "/usr/local/lib/python3.6/asyncio/queues.py", line 169, in get > getter.cancel() # Just in case getter is not done yet. > File "/usr/local/lib/python3.6/asyncio/base_events.py", line > 574, in call_soon > self._check_closed() > File "/usr/local/lib/python3.6/asyncio/base_events.py", line > 357, in _check_closed > raise RuntimeError('Event loop is closed') > RuntimeError: Event loop is closed > > Calling loop.shutdown_asyncgens() made the error go away, but it seems > a little obscure that by adding an asynchronous iterator somewhere in > your code, you have to remember to check that that line is present > before loop.close() is called (and the exception message doesn't > provide a good hint). > > Is there any disadvantage to always calling loop.shutdown_asyncgens() > (i.e. even if it's not needed)? And why might someone need to call it > at a different time? > > Thanks, > --Chris > _______________________________________________ > Async-sig mailing list > [email protected] > https://mail.python.org/mailman/listinfo/async-sig > Code of Conduct: https://www.python.org/psf/codeofconduct/
_______________________________________________ Async-sig mailing list [email protected] https://mail.python.org/mailman/listinfo/async-sig Code of Conduct: https://www.python.org/psf/codeofconduct/
