Exception “ There is no current event loop in thread 'MainThread' ” while running over new loop

浪子不回头ぞ 提交于 2019-11-30 20:59:30

I want to know it is normal for asyncio that main loop Must be set and is used for coroutines regardless of a loop over which coroutine is run.

This used to be required prior to Python 3.6. The reason is that functions like asyncio.sleep() need an event loop to be able to use loop.call_later() to schedule a wake-up call to complete the future.

As of Python 3.6 (or 3.5.3, which included a fix for the issue), when get_event_loop() is invoked from a coroutine driven by an event loop, it always returns the event loop that drives it. As a result, your code works correctly.

The new behavior is not mentioned in the online documentation, but is in the docstring:

When called from a coroutine or a callback (e.g. scheduled with call_soon or similar API), this function will always return the running event loop.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!