【Jupyter】異步RuntimeError: asyncio.run() cannot be called from a running event loop


問題:在jupyter notebook中使用asyncio.run()時發生如上標題報錯,沒錯就是這個

 

 官方文檔:This function cannot be called when another asyncio event loop is running in the same thread.

百度翻譯:當另一個異步事件循環在同一線程中運行時,無法調用此函數

 

大致就是jupyter 已經運行了loop,無需自己激活,采用上文中的await()調用即可

In jupyter

async def main():
        print(1)
await main()

In plain Python (≥3.7)

import asyncio
async def main():
    print(1)
asyncio.run(main())

鏈接: 原文

參考:https://blog.csdn.net/sunnydarkcloud/article/details/101775608


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM