如下圖,在使用 asyncio 呼叫事件回圈 loop 時出現錯誤 RuntimeError: This event loop is already running:
import asyncio
async def execute(x):
print('Number:', x)
coroutine = execute(1)
print('Coroutine:', coroutine)
print('After calling execute')
loop = asyncio.get_event_loop()
loop.run_until_complete(coroutine)
print('After calling loop')
運行程式時出現錯誤 RuntimeError: This event loop is already running:

查閱資料后,發現所使用的 Python 編輯器為 Spyder,其連接著 IPython 內核,而 IPython 內核本身在事件回圈上運行,而 asyncio 不允許嵌套其事件??回圈,因此會出現如上圖的錯誤資訊,
切換到 Pycharm 編輯器即可解決問題:

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/161685.html
標籤:其他
