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


成功解决!

目录
打赏
0
0
0
0
11
分享
相关文章
|
9月前
|
python RuntimeError: main thread is not in main loop
python RuntimeError: main thread is not in main loop
209 1
|
9月前
|
关于thread使用的错误:pure virtual method called terminate called without an active exception
关于thread使用的错误:pure virtual method called terminate called without an active exception
184 1
已解决 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'.异常错误,已解决
676 0
已解决 RuntimeError: There is no current event loop in thread ‘Thread-1‘.
Traceback(most recent call last):File "main.py", line 4l,in<module>alueError: sleep length must be n
Traceback(most recent call last):File "main.py", line 4l,in<module>alueError: sleep length must be n
解决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()”
1304 0
|
9月前
|
python tkinter Tcl_AsyncDelete: async handler deleted by the wrong thread
python tkinter Tcl_AsyncDelete: async handler deleted by the wrong thread
149 1
androidstudio无法启动,Failed to create a child event loop
androidstudio无法启动,Failed to create a child event loop
131 0
“RuntimeError: main thread is not in main loop“的几种解决方案
“RuntimeError: main thread is not in main loop“的几种解决方案
|
9月前
【Bug】ERROR ResizeObserver loop completed with undelivered notifications.
【Bug】ERROR ResizeObserver loop completed with undelivered notifications.
完美解决丨RuntimeError: create_session() called before __init__().
完美解决丨RuntimeError: create_session() called before __init__().

热门文章

最新文章