From 84c5edf5a01899559e114c27d8b2dba220de24ee Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Wed, 15 Jan 2025 00:13:42 +0700 Subject: [PATCH] stop reader thread on unsubscribe --- app.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 0941d3b..458d0de 100644 --- a/app.py +++ b/app.py @@ -216,6 +216,7 @@ async def detect(websocket: WebSocket): try: msg = await websocket.receive_text() logging.debug(f"Received message: {msg}") + print(f"Received message: {msg}") data = json.loads(msg) msg_type = data.get("type") @@ -288,6 +289,8 @@ async def detect(websocket: WebSocket): with streams_lock: if camera_id and camera_id in streams: stream = streams.pop(camera_id) + stream['stop_event'].set() + stream['thread'].join() stream['cap'].release() logging.info(f"Unsubscribed from camera {camera_id}") if camera_id in models and modelId in models[camera_id]: @@ -357,4 +360,4 @@ async def detect(websocket: WebSocket): streams.clear() with models_lock: models.clear() - logging.info("WebSocket connection closed") + logging.info("WebSocket connection closed") \ No newline at end of file