【ERROR】asyncio.run(main())报错:RuntimeError: Event loop is closed

简介: 【ERROR】asyncio.run(main())报错:RuntimeError: Event loop is closed
if __name__ == '__main__':
    asyncio.run(main())


python协程报错:RuntimeError: Event loop is closed


错误原因:asyncio.run()会自动关闭循环,并且调用_ProactorBasePipeTransport.__del__报错, 而asyncio.run_until_complete()不会


解决方法:换成下边的


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())


参考链接:https://blog.csdn.net/weixin_46830352/article/details/121372542


报警告:DeprecationWarning: There is no current event loop loop = asyncio.get_event_loop()


警告原因:python版本过高(>=3.10),在代码中使用旧的方式获得loop


解决:换成下边的


if __name__ == '__main__':
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(main())


成功解决!

目录
相关文章
已解决 RuntimeError: There is no current event loop in thread ‘Thread-1‘.
Jetson Xavier NX 报错 RuntimeError: There is no current event loop in thread 'Thread-1'.异常错误,已解决
465 0
已解决 RuntimeError: There is no current event loop in thread ‘Thread-1‘.
|
3月前
|
网络安全
androidstudio无法启动,Failed to create a child event loop
androidstudio无法启动,Failed to create a child event loop
89 0
|
5月前
|
Python
“RuntimeError: main thread is not in main loop“的几种解决方案
“RuntimeError: main thread is not in main loop“的几种解决方案
|
6月前
|
TensorFlow 算法框架/工具 Python
完美解决丨RuntimeError: create_session() called before __init__().
完美解决丨RuntimeError: create_session() called before __init__().
|
11月前
|
前端开发
The following tasks did not complete: first Did you forget to signal async completion?
The following tasks did not complete: first Did you forget to signal async completion?
Error:svn:E155037:Previous operation has not finished; run ‘cleanup‘ if it was interrupted(完美解决)
Error:svn:E155037:Previous operation has not finished; run ‘cleanup‘ if it was interrupted(完美解决)
411 0
Error:svn:E155037:Previous operation has not finished; run ‘cleanup‘ if it was interrupted(完美解决)
|
Python
may have been in progress in another thread when fork() was called.
may have been in progress in another thread when fork() was called.
139 0
may have been in progress in another thread when fork() was called.
(Python)asyncio使用异常:This event loop is already running解决方式
(Python)asyncio使用异常:This event loop is already running解决方式