From a1b7795af6095cd821a3bc279ec2fcc9a03b3b98 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期二, 29 十月 2024 21:30:22 +0800 Subject: [PATCH] 代码优化 --- app/api/report.py | 27 ++++++++++++++++----------- 1 files changed, 16 insertions(+), 11 deletions(-) diff --git a/app/api/report.py b/app/api/report.py index d262013..254695d 100644 --- a/app/api/report.py +++ b/app/api/report.py @@ -83,18 +83,23 @@ 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: - 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") + # 鍙栨秷鎵�鏈変换鍔� + for task in tasks: + if not task.done(): + task.cancel() + try: + await task + except asyncio.CancelledError: + pass @router.get("/variables/list", response_model=ResponseList) -- Gitblit v1.8.0