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

Event loop created by asyncio.new_event_loop hangs

下面的代码没有打印任何东西就挂了:

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 一切正常。是我做错了什么还是偶然发现了错误?

我正在使用 Python 3.5.1.

asyncio.AbstractEventLoopPolicy.new_event_loop documentation 说:

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