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


成功解决!

目录
相关文章
|
7月前
|
Python
python RuntimeError: main thread is not in main loop
python RuntimeError: main thread is not in main loop
194 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'.异常错误,已解决
618 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()”
1267 0
|
6月前
|
Python
“RuntimeError: main thread is not in main loop“的几种解决方案
“RuntimeError: main thread is not in main loop“的几种解决方案
|
前端开发
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(完美解决)
436 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.
142 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解决方式

热门文章

最新文章