简体   繁体   English

由asyncio.new_event_loop创建的事件循环挂起

[英]Event loop created by asyncio.new_event_loop hangs

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()

If I use get_event_loop everything works fine. 如果我使用get_event_loop一切正常。 Is there something I'm doing wrong or have I stumbled upon a bug? 有什么我做错了或我偶然发现了一个错误?

I'm using Python 3.5.1. 我正在使用Python 3.5.1。

asyncio.AbstractEventLoopPolicy.new_event_loop documentation says: asyncio.AbstractEventLoopPolicy.new_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. 如果需要将此循环设置为当前上下文的事件循环,则必须显式调用set_event_loop()


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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM