stop reader thread on unsubscribe

This commit is contained in:
Siwat Sirichai 2025-01-15 00:13:42 +07:00
parent ffe9c90747
commit 84c5edf5a0

5
app.py
View file

@ -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")