After first run ,Jupyter notebook with python 3.6.1, using asyncio basic example gives: RuntimeError: Event loop is closed
After first run ,Jupyter notebook with python 3.6.1, using asyncio basic example gives: RuntimeError: Event loop is closed
在 Jupyter Notebook (python 3.6.1) 中,我去了 运行 基本 python 文档 Hello_World (18.5.3.1.1。示例:你好World 协程)并注意到它给了我一个 RuntimeError。在尝试了很长时间发现程序的问题之后(我的理解是文档可能不是完全最新的),我终于注意到它只在第二个 运行 之后才这样做,并在重新启动的内核中进行了测试.从那以后,我在两个连续的单元格(在 1 和 2 中)中复制了相同的小 python 程序,发现它在第二个单元格而不是第一个单元格中给出了错误,并且在之后的两个单元格中给出了错误。重启内核后重复此操作。
import asyncio
def hello_world(loop):
print('Hello World')
loop.stop()
loop = asyncio.get_event_loop()
# Schedule a call to hello_world()
loop.call_soon(hello_world, loop)
# Blocking call interrupted by loop.stop()
loop.run_forever()
loop.close()
回溯:
RuntimeError Traceback (most recent call last)
<ipython-input-2-0930271bd896> in <module>()
6 loop = asyncio.get_event_loop()
7 # Blocking call which returns when the hello_world() coroutine
----> 8 loop.run_until_complete(hello_world())
9 loop.close()
/home/pontiac/anaconda3/lib/python3.6/asyncio/base_events.py in run_until_complete(self, future)
441 Return the Future's result, or raise its exception.
442 """
--> 443 self._check_closed()
444
445 new_task = not futures.isfuture(future)
/home/pontiac/anaconda3/lib/python3.6/asyncio/base_events.py in _check_closed(self)
355 def _check_closed(self):
356 if self._closed:
--> 357 raise RuntimeError('Event loop is closed')
358
359 def _asyncgen_finalizer_hook(self, agen):
RuntimeError: Event loop is closed
当 运行在解释器中设置了所有调试设置的文件时,我没有收到此错误。我正在 运行 在我最近重新安装的 Anaconda 设置中安装此笔记本,该设置仅安装了 3.6.1 python 版本。
问题是 loop.close() 使循环无法供将来使用。也就是说,在调用 close 之后,您将永远无法再次使用循环。循环作为一个对象保留下来,但是一旦循环关闭,循环上的几乎所有方法都会引发异常。但是,如果多次调用 asyncio.get_event_loop() returns 相同的循环。您经常需要这样做,以便应用程序的多个部分获得相同的事件循环。
但是,如果您计划关闭循环,则最好调用 asyncio.new_event_loop 而不是 asyncio.get_event_loop。这会给你一个新的事件循环。如果您调用 new_event_loop 而不是 get_event_loop,您有责任确保在该线程中 运行 的应用程序的所有部分都使用了正确的循环。如果您希望能够 运行 多次进行测试,您可以执行以下操作:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
之后,你会发现asyncio.get_event_loop returns和循环是一样的。因此,如果您在程序顶部附近执行此操作,您将在代码的每个 运行 处都有一个新的事件循环。
在 Jupyter Notebook (python 3.6.1) 中,我去了 运行 基本 python 文档 Hello_World (18.5.3.1.1。示例:你好World 协程)并注意到它给了我一个 RuntimeError。在尝试了很长时间发现程序的问题之后(我的理解是文档可能不是完全最新的),我终于注意到它只在第二个 运行 之后才这样做,并在重新启动的内核中进行了测试.从那以后,我在两个连续的单元格(在 1 和 2 中)中复制了相同的小 python 程序,发现它在第二个单元格而不是第一个单元格中给出了错误,并且在之后的两个单元格中给出了错误。重启内核后重复此操作。
import asyncio
def hello_world(loop):
print('Hello World')
loop.stop()
loop = asyncio.get_event_loop()
# Schedule a call to hello_world()
loop.call_soon(hello_world, loop)
# Blocking call interrupted by loop.stop()
loop.run_forever()
loop.close()
回溯:
RuntimeError Traceback (most recent call last)
<ipython-input-2-0930271bd896> in <module>()
6 loop = asyncio.get_event_loop()
7 # Blocking call which returns when the hello_world() coroutine
----> 8 loop.run_until_complete(hello_world())
9 loop.close()
/home/pontiac/anaconda3/lib/python3.6/asyncio/base_events.py in run_until_complete(self, future)
441 Return the Future's result, or raise its exception.
442 """
--> 443 self._check_closed()
444
445 new_task = not futures.isfuture(future)
/home/pontiac/anaconda3/lib/python3.6/asyncio/base_events.py in _check_closed(self)
355 def _check_closed(self):
356 if self._closed:
--> 357 raise RuntimeError('Event loop is closed')
358
359 def _asyncgen_finalizer_hook(self, agen):
RuntimeError: Event loop is closed
当 运行在解释器中设置了所有调试设置的文件时,我没有收到此错误。我正在 运行 在我最近重新安装的 Anaconda 设置中安装此笔记本,该设置仅安装了 3.6.1 python 版本。
问题是 loop.close() 使循环无法供将来使用。也就是说,在调用 close 之后,您将永远无法再次使用循环。循环作为一个对象保留下来,但是一旦循环关闭,循环上的几乎所有方法都会引发异常。但是,如果多次调用 asyncio.get_event_loop() returns 相同的循环。您经常需要这样做,以便应用程序的多个部分获得相同的事件循环。 但是,如果您计划关闭循环,则最好调用 asyncio.new_event_loop 而不是 asyncio.get_event_loop。这会给你一个新的事件循环。如果您调用 new_event_loop 而不是 get_event_loop,您有责任确保在该线程中 运行 的应用程序的所有部分都使用了正确的循环。如果您希望能够 运行 多次进行测试,您可以执行以下操作:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
之后,你会发现asyncio.get_event_loop returns和循环是一样的。因此,如果您在程序顶部附近执行此操作,您将在代码的每个 运行 处都有一个新的事件循环。