[New-bugs-announce] [issue21326] asyncio: request clearer error message when event loop closed

Mark Dickinson report at bugs.python.org
Tue Apr 22 10:12:43 CEST 2014


New submission from Mark Dickinson:

In the new asyncio library, it's easy for newbies (like me) to accidentally try to run a coroutine on a closed event loop.  Doing so leads to a rather inscrutable exception and traceback:

>>> loop.run_until_complete(compute())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/asyncio/base_events.py", line 203, in run_until_complete
    self.run_forever()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/asyncio/base_events.py", line 184, in run_forever
    self._run_once()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/asyncio/base_events.py", line 778, in _run_once
    event_list = self._selector.select(timeout)
AttributeError: 'NoneType' object has no attribute 'select'

Is it possible to replace this with something clearer?  For example, something like: RuntimeError("Can't schedule coroutine on closed event loop.")


Here's the full code snippet:


Python 3.4.0 (default, Mar 25 2014, 11:07:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> @asyncio.coroutine
... def compute():
...     print("Starting computation")
...     yield from asyncio.sleep(2.0)
...     print("Complete")
... 
>>> loop = asyncio.get_event_loop()
>>> loop.run_until_complete(compute())
Starting computation
Complete
>>> loop.close()  # whoops
>>> # some time later
... 
>>> loop = asyncio.get_event_loop()
>>> loop.run_until_complete(compute())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/asyncio/base_events.py", line 203, in run_until_complete
    self.run_forever()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/asyncio/base_events.py", line 184, in run_forever
    self._run_once()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/asyncio/base_events.py", line 778, in _run_once
    event_list = self._selector.select(timeout)
AttributeError: 'NoneType' object has no attribute 'select'

----------
components: Library (Lib)
messages: 216994
nosy: gvanrossum, mark.dickinson
priority: normal
severity: normal
stage: needs patch
status: open
title: asyncio: request clearer error message when event loop closed
type: enhancement
versions: Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21326>
_______________________________________


More information about the New-bugs-announce mailing list