原来使用:html
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(do_work(checker))
loop.run_until_complete(asyncio.wait([task]))
st = task.result()
修改后使用:
添加了
new_loop = asyncio.new_event_loop() asyncio.set_event_loop(new_loop)
添加后以下:
new_loop = asyncio.new_event_loop() asyncio.set_event_loop(new_loop) loop = asyncio.get_event_loop() task = asyncio.ensure_future(do_work(checker)) loop.run_until_complete(asyncio.wait([task])) st = task.result()
根本缘由在源码中:python
def get_event_loop(self): """Get the event loop. This may be None or an instance of EventLoop. """ if (self._local._loop is None and not self._local._set_called and isinstance(threading.current_thread(), threading._MainThread)): self.set_event_loop(self.new_event_loop()) if self._local._loop is None: raise RuntimeError('There is no current event loop in thread %r.' % threading.current_thread().name) return self._local._loop
在主线程中,调用get_event_loop总能返回属于主线程的event loop对象,若是是处于非主线程中,还须要调用set_event_loop方法指定一个event loop对象,这样get_event_loop才会获取到被标记的event loop对象:git
def set_event_loop(self, loop): """Set the event loop.""" self._local._set_called = True assert loop is None or isinstance(loop, AbstractEventLoop) self._local._loop = loop
官方文档: https://docs.python.org/3/library/asyncio-eventloop.html
事件循环
源代码: Lib / asyncio / events.py, Lib / asyncio / base_events.pygithub
前言网络
事件循环是每一个异步应用程序的核心。事件循环运行异步任务和回调,执行网络IO操做并运行子流程。框架
应用程序开发人员一般应使用高级异步函数(例如)asyncio.run()
,而且几乎不须要引用循环对象或调用其方法。本部分主要面向须要更好地控制事件循环行为的较低级代码,库和框架的做者。异步
获取事件循环async
如下低级函数可用于获取,设置或建立事件循环:函数
-
asyncio.
get_running_loop
() -
返回当前OS线程中的运行事件循环。oop
若是没有正在运行的事件循环,
RuntimeError
则会引起一个。只能从协程或回调中调用此函数。3.7版中的新功能。
-
asyncio.
get_event_loop
() -
获取当前事件循环。若是当前OS线程
set_event_loop()
中没有设置当前事件循环而且还没有调用,那么asyncio将建立一个新的事件循环并将其设置为当前事件循环。由于此函数具备至关复杂的行为(尤为是在使用自定义事件循环策略时),因此 在协程和回调中
get_running_loop()
首选使用此 函数get_event_loop()
。还能够考虑使用该
asyncio.run()
函数,而不要使用较低级别的函数来手动建立和关闭事件循环。
-
asyncio.
set_event_loop
(循环) -
将loop设置为当前OS线程的当前事件循环。
-
asyncio.
new_event_loop
() -
建立一个新的事件循环对象。
须要注意的是行为get_event_loop()
,set_event_loop()
和new_event_loop()
功能均可以经过改变 设置自定义事件循环政策。
asyncio 官方文档地址https://docs.python.org/3/library/asyncio.html