简体   繁体   English

在Python3.6.1中调用loop.close asyncio.get_event_loop后无法创建新的事件循环

[英]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.

相关问题 asyncio.get_event_loop(): DeprecationWarning: 没有当前事件循环 - asyncio.get_event_loop(): DeprecationWarning: There is no current event loop 如何替换 asyncio.get_event_loop() 以避免 DeprecationWarning? - How to replace asyncio.get_event_loop() to avoid the DeprecationWarning? 如果有两个 asyncio.get_event_loop,顺序是什么? - what's the order if there are two asyncio.get_event_loop? 检查 asyncio.get_event_loop() 是否已完成? - Check if asyncio.get_event_loop() has finished? 我是否需要跟踪 asyncio 事件循环,或者我可以在需要时调用 asyncio.get_event_loop 吗? - Do I need to keep track of the asyncio event loop, or can I just call asyncio.get_event_loop when it is needed? 何时使用 asyncio.get_running_loop() 与 asyncio.get_event_loop()? - When to use asyncio.get_running_loop() vs asyncio.get_event_loop()? 跟随 asyncio.run() 时 asyncio.get_event_loop() 失败 - asyncio.get_event_loop() fails when following asyncio.run() py3.10之后的版本如何实现asyncio.get_event_loop与之前版本行为相同 - How do versions after py3.10 implement asyncio.get_event_loop with the same behavior as previous versions 为什么asyncio.get_event_loop方法检查当前线程是否为主线程? - Why asyncio.get_event_loop method checks if the current thread is the main thread? 在没有 asyncio.get_event_loop() 的 run_in_executor 的异步方法中使用线程池 - Using Threadpool in an Async method without run_in_executor of asyncio.get_event_loop()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM