asyncio.new_event_loop 创建的事件循环挂起 [英] Event loop created by asyncio.new_event_loop hangs
本文介绍了asyncio.new_event_loop 创建的事件循环挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码只是挂起而没有打印任何内容:
The following code just hangs without ever printing anything:
import asyncio
async def foo(loop):
print('foo')
loop.stop()
loop = asyncio.new_event_loop()
asyncio.ensure_future(foo(loop))
loop.run_forever()
如果我使用 get_event_loop 一切正常.是我做错了什么还是我偶然发现了一个错误?
If I use get_event_loop everything works fine. Is there something I'm doing wrong or have I stumbled upon a bug?
我使用的是 Python 3.5.1.
I'm using Python 3.5.1.
推荐答案
asyncio.AbstractEventLoopPolicy.new_event_loop
文档 说:
如果需要将此循环设置为当前的事件循环上下文,set_event_loop()
必须显式调用.
If there’s need to set this loop as the event loop for the current context,
set_event_loop()
must be called explicitly.
<小时>
import asyncio
async def foo(loop):
print('foo')
loop.stop()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop) # <----
asyncio.ensure_future(foo(loop))
loop.run_forever()
这篇关于asyncio.new_event_loop 创建的事件循环挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文