瀏覽代碼

异步恢复中断的流程

jihuaqiang 1 月之前
父節點
當前提交
ee5b8ac8b6
共有 1 個文件被更改,包括 10 次插入1 次删除
  1. 10 1
      agent.py

+ 10 - 1
agent.py

@@ -113,12 +113,21 @@ async def lifespan(app: FastAPI):
     identify_tool = IdentifyTool()
     
     # 启动后恢复中断的流程
-    await restore_interrupted_processes()
+    # 异步恢复中断流程,避免阻塞启动
+    app.state.restore_task = asyncio.create_task(restore_interrupted_processes())
     
     yield
     
     # 关闭时执行
     logger.info("🛑 关闭 Knowledge Agent 服务...")
+    # 优雅取消后台恢复任务
+    restore_task = getattr(app.state, 'restore_task', None)
+    if restore_task and not restore_task.done():
+        restore_task.cancel()
+        try:
+            await restore_task
+        except asyncio.CancelledError:
+            logger.info("✅ 已取消后台恢复任务")
 
 async def restore_interrupted_processes():
     """