fix: 修复关闭报错

This commit is contained in:
tcmofashi
2025-03-27 14:01:20 +08:00
parent 3caac382a5
commit d3b4ca30da
3 changed files with 13 additions and 7 deletions

View File

@@ -49,8 +49,11 @@ class BaseMessageAPI:
"""异步方式运行服务器"""
config = uvicorn.Config(self.app, host=self.host, port=self.port, loop="asyncio")
self.server = uvicorn.Server(config)
await self.server.serve()
try:
await self.server.serve()
except KeyboardInterrupt as e:
await self.stop()
raise KeyboardInterrupt from e
async def start_server(self):
"""启动服务器的异步方法"""
@@ -83,4 +86,4 @@ class BaseMessageAPI:
loop.close()
global_api = BaseMessageAPI(host=os.environ["HOST"], port=os.environ["PORT"])
global_api = BaseMessageAPI(host=os.environ["HOST"], port=int(os.environ["PORT"]))