[Async-sig] question re: loop.shutdown_asyncgens()

Chris Jerdonek chris.jerdonek at gmail.com
Thu Jul 27 23:24:15 EDT 2017


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


More information about the Async-sig mailing list