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


成功解决!

目录
相关文章
|
9月前
|
Python
python RuntimeError: main thread is not in main loop
python RuntimeError: main thread is not in main loop
209 1
|
9月前
|
NoSQL 编译器 API
关于thread使用的错误:pure virtual method called terminate called without an active exception
关于thread使用的错误:pure virtual method called terminate called without an active exception
191 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'.异常错误,已解决
677 0
已解决 RuntimeError: There is no current event loop in thread ‘Thread-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()”
1304 0
|
9月前
|
Python
python tkinter Tcl_AsyncDelete: async handler deleted by the wrong thread
python tkinter Tcl_AsyncDelete: async handler deleted by the wrong thread
149 1
|
6月前
|
网络安全
androidstudio无法启动,Failed to create a child event loop
androidstudio无法启动,Failed to create a child event loop
132 0
|
8月前
|
Python
“RuntimeError: main thread is not in main loop“的几种解决方案
“RuntimeError: main thread is not in main loop“的几种解决方案
|
9月前
|
TensorFlow 算法框架/工具 Python
完美解决丨RuntimeError: create_session() called before __init__().
完美解决丨RuntimeError: create_session() called before __init__().
|
前端开发
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?
|
Java Spring
Redisson BUG: Failed to submit a listener notification task. Event loop shut down?
Redisson BUG: Failed to submit a listener notification task. Event loop shut down?
1543 0