Message216994
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' |
|
Date |
User |
Action |
Args |
2014-04-22 08:12:43 | mark.dickinson | set | recipients:
+ mark.dickinson, gvanrossum |
2014-04-22 08:12:42 | mark.dickinson | set | messageid: <1398154363.0.0.853788262895.issue21326@psf.upfronthosting.co.za> |
2014-04-22 08:12:42 | mark.dickinson | link | issue21326 messages |
2014-04-22 08:12:42 | mark.dickinson | create | |
|