fix: 修复处理消息时占用连接的情况

This commit is contained in:
tcmofashi
2025-03-29 00:45:00 +08:00
parent 33d794cd1f
commit 19d2aaed52
2 changed files with 21 additions and 6 deletions

View File

@@ -12,6 +12,7 @@ class BaseMessageAPI:
self.host = host
self.port = port
self.message_handlers: List[Callable] = []
self.cache = []
self._setup_routes()
self._running = False
@@ -20,12 +21,11 @@ class BaseMessageAPI:
@self.app.post("/api/message")
async def handle_message(message: Dict[str, Any]):
# try:
for handler in self.message_handlers:
await handler(message)
return {"status": "success"}
# except Exception as e:
# raise HTTPException(status_code=500, detail=str(e)) from e
try:
self.cache.append(message)
return {"status": "success"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) from e
def register_message_handler(self, handler: Callable):
"""注册消息处理函数"""
@@ -41,6 +41,20 @@ class BaseMessageAPI:
# logger.error(f"发送消息失败: {str(e)}")
pass
async def message_process(
self,
):
"""启动消息处理"""
while True:
if len(self.cache) > 0:
for handler in self.message_handlers:
await handler(self.cache[0])
self.cache.pop(0)
if len(self.cache) > 0:
await asyncio.sleep(0.1 / len(self.cache))
else:
await asyncio.sleep(0.2)
def run_sync(self):
"""同步方式运行服务器"""
uvicorn.run(self.app, host=self.host, port=self.port)