[英]Can't create new event loop after calling loop.close asyncio.get_event_loop in Python3.6.1
In Python3.6.1 after calling loop.close() on a loop obtained from asyncio.get_event_loop(), can a new loop be created? 在从asyncio.get_event_loop()获取的循环上调用loop.close()之后的Python3.6.1中,是否可以创建一个新循环?
I've looked at other posts with answers for properly closing a loop, and also how to use task.cancel(), but have not been able to use any of those examples in a way that would let a new loop be created after the first was closed. 我已经查看了其他帖子,其中包含正确关闭循环的答案,以及如何使用task.cancel(),但是未能以某种方式使用这些示例,这样可以在第一次被关闭。 I also tried explicitly setting executor and later calling executor.shutdown(wait=True), but that didn't help.
我也尝试显式设置执行程序,然后调用executor.shutdown(wait = True),但这没有帮助。 I also tried 'del loop', and del a bunch of other things.
我也试过'del loop',并尝试了其他一些东西。
Documentation indicates that closing a an event loop is idempotent and irreversible. 文档表明关闭一个事件循环是幂等的,不可逆转的。 Does that also mean that a new loop cannot be created?
这是否意味着无法创建新循环?
Here is some simple example code to demonstrate the issue: ` 这是一些简单的示例代码来演示这个问题:`
#!/usr/bin/env python3.6 ''' To demonstrate an issue, the following code was adapted from: https://docs.python.org/3/library/asyncio-eventloop.html ''' import asyncio def hello_world(loop): print('Hello World') loop.stop() loop = asyncio.get_event_loop() loop.call_soon(hello_world, loop) loop.run_forever() # loop.close() ''' If the commented out loop.close() above is uncommented, the following code will fail with: Traceback (most recent call last): File "./aquestion.py", line 28, in <module> loopNew.call_soon(hello_world, loopNew) File "/Library/Frameworks/Python.framework/Versions/3.6/lib /python3.6/asyncio/base_events.py", line 573, in call_soon self._check_closed() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py", line 357, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed ''' loopNew = asyncio.get_event_loop() loopNew.call_soon(hello_world, loopNew) loopNew.run_forever()
`
`
Any attempts to answer my question would be appreciated. 任何试图回答我的问题将不胜感激。
Alternatively, would it be bad form to create an event loop, use it for a variety of purposes, and then just close that loop when the long running program is about to exit? 或者,创建一个事件循环,将它用于各种目的,然后在长时间运行的程序即将退出时关闭该循环是不好的形式? This just seems wrong.
这似乎是错的。
asyncio.get_event_loop
returns the current loop. asyncio.get_event_loop
返回当前循环。 It pays no attention to the state of the loop. 它不关注循环的状态。 If you need a new loop after closing one, you can use
asyncio.new_event_loop
. 如果在关闭之后需要一个新循环,则可以使用
asyncio.new_event_loop
。
Be aware that getting a new loop won't affect subsequent calls to get_event_loop
. 请注意,获取新循环不会影响对
get_event_loop
后续调用。 If you'd like that to return your new loop instead of the original one (especially since you've probably closed it), you'll need to call asyncio.set_event_loop
yourself. 如果您希望返回新循环而不是原始循环(特别是因为您可能已关闭它),您需要自己调用
asyncio.set_event_loop
。
import asyncio
async def f():
await asyncio.sleep(0)
loop = asyncio.get_event_loop()
loop.run_until_complete(f())
loop.close()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.