zhaoqingang
2024-11-07 50f9b062456bd595d4fee86e7c90e0cac8904960
app/api/report.py
@@ -76,13 +76,22 @@
                        await websocket.send_json(result)
                        print(f"Forwarded to client, {chat_id}: {result}")
                    last_message = "step" if steps else "message"
                    last_message = "message" if msg else "step"
            # 启动两个任务,分别处理客户端和服务端的消息
            tasks = [
                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():