From 976f9731921853d3098729e4ba8ef6b2d377e9e7 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期二, 29 十月 2024 22:40:38 +0800
Subject: [PATCH] 代码优化
---
app/api/chat.py | 45 +++++++++++++++++++++++++++++++++++++--------
app/api/report.py | 11 ++++++++++-
2 files changed, 47 insertions(+), 9 deletions(-)
diff --git a/app/api/chat.py b/app/api/chat.py
index 7bb7d8a..f11b01e 100644
--- a/app/api/chat.py
+++ b/app/api/chat.py
@@ -23,6 +23,7 @@
chat_id: str,
current_user: UserModel = Depends(get_current_user_websocket),
db: Session = Depends(get_db)):
+ tasks = []
await websocket.accept()
print(f"Client {agent_id} connected")
@@ -87,18 +88,31 @@
print(f"Error decode ragflow response: {text}")
pass
- except Exception as e:
- result = {"message": f"鍐呴儴閿欒锛� {e}", "type": "close"}
+ except Exception as e2:
+ result = {"message": f"鍐呴儴閿欒锛� {e2}", "type": "close"}
await websocket.send_json(result)
- print(f"Error process message of ragflow: {e}")
-
+ print(f"Error process message of ragflow: {e2}")
# 鍚姩浠诲姟澶勭悊瀹㈡埛绔秷鎭�
tasks = [
asyncio.create_task(forward_to_ragflow())
]
await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
- except WebSocketDisconnect:
- print(f"Client {chat_id} disconnected")
+ except WebSocketDisconnect as e1:
+ print(f"Client {chat_id} disconnected: {e1}")
+ await websocket.close()
+ except Exception as e:
+ print(f"Exception occurred: {e}")
+
+ finally:
+ print("Cleaning up resources of ragflow")
+ # 鍙栨秷鎵�鏈変换鍔�
+ for task in tasks:
+ if not task.done():
+ task.cancel()
+ try:
+ await task
+ except asyncio.CancelledError:
+ pass
elif agent_type == AgentType.BISHENG:
token = get_bisheng_token(db, current_user.id)
@@ -155,8 +169,23 @@
except asyncio.CancelledError:
pass
- except WebSocketDisconnect:
- print(f"Client {chat_id} disconnected")
+ except WebSocketDisconnect as e:
+ print(f"WebSocket connection closed with code {e.code}: {e.reason}")
+ await websocket.close()
+ await service_websocket.close()
+ except Exception as e:
+ print(f"Exception occurred: {e}")
+
+ finally:
+ print("Cleaning up resources of bisheng")
+ # 鍙栨秷鎵�鏈変换鍔�
+ for task in tasks:
+ if not task.done():
+ task.cancel()
+ try:
+ await task
+ except asyncio.CancelledError:
+ pass
else:
ret = {"message": "Agent not found", "type": "close"}
await websocket.send_json(ret)
diff --git a/app/api/report.py b/app/api/report.py
index 254695d..22c9bf6 100644
--- a/app/api/report.py
+++ b/app/api/report.py
@@ -83,6 +83,15 @@
asyncio.create_task(forward_to_service()),
asyncio.create_task(forward_to_client())
]
+ done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
+
+ # 鍙栨秷鏈畬鎴愮殑浠诲姟
+ for task in pending:
+ task.cancel()
+ try:
+ await task
+ except asyncio.CancelledError:
+ pass
except WebSocketDisconnect as e:
print(f"WebSocket connection closed with code {e.code}: {e.reason}")
@@ -91,7 +100,7 @@
except Exception as e:
print(f"Exception occurred: {e}")
finally:
- print("Cleaning up resources")
+ print("Cleaning up resources of bisheng report")
# 鍙栨秷鎵�鏈変换鍔�
for task in tasks:
if not task.done():
--
Gitblit v1.8.0