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())
成功解决!