原來使用:
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()
根本原因在源碼中:
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對象:
官方文檔: https://docs.python.org/3/library/asyncio-eventloop.html
事件循環
源代碼: Lib / asyncio / events.py, Lib / asyncio / base_events.py
前言
事件循環是每個異步應用程序的核心。事件循環運行異步任務和回調,執行網絡IO操作並運行子流程。
應用程序開發人員通常應使用高級異步函數(例如)asyncio.run()
,並且幾乎不需要引用循環對象或調用其方法。本部分主要面向需要更好地控制事件循環行為的較低級代碼,庫和框架的作者。
獲取事件循環
以下低級函數可用於獲取,設置或創建事件循環:
-
asyncio.
get_running_loop
() -
返回當前OS線程中的運行事件循環。
如果沒有正在運行的事件循環,
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