【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())


成功解决!

目录
相关文章
|
11月前
已解决 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'.异常错误,已解决
187 0
已解决 RuntimeError: There is no current event loop in thread ‘Thread-1‘.
|
13天前
|
Python
“RuntimeError: main thread is not in main loop“的几种解决方案
“RuntimeError: main thread is not in main loop“的几种解决方案
9 1
|
2月前
|
Python
python tkinter Tcl_AsyncDelete: async handler deleted by the wrong thread
python tkinter Tcl_AsyncDelete: async handler deleted by the wrong thread
74 1
|
开发工具 Android开发
解决bug:运行项目时报异常 “Can't create handler inside thread that has not called Looper.prepare()”
解决bug:运行项目时报异常 “Can't create handler inside thread that has not called Looper.prepare()”
952 0
|
2月前
|
TensorFlow 算法框架/工具 Python
完美解决丨RuntimeError: create_session() called before __init__().
完美解决丨RuntimeError: create_session() called before __init__().
|
7月前
|
前端开发
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(完美解决)
323 0
Error:svn:E155037:Previous operation has not finished; run ‘cleanup‘ if it was interrupted(完美解决)
(Python)asyncio使用异常:This event loop is already running解决方式
(Python)asyncio使用异常:This event loop is already running解决方式