【ERROR】asyncio_run(main()) 报错:RuntimeError: Event loop is closed
小于 1 分钟
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())
成功解决!