【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