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: try:
msg = await websocket.receive_text() msg = await websocket.receive_text()
logging.debug(f"Received message: {msg}") logging.debug(f"Received message: {msg}")
print(f"Received message: {msg}")
data = json.loads(msg) data = json.loads(msg)
msg_type = data.get("type") msg_type = data.get("type")
@ -288,6 +289,8 @@ async def detect(websocket: WebSocket):
with streams_lock: with streams_lock:
if camera_id and camera_id in streams: if camera_id and camera_id in streams:
stream = streams.pop(camera_id) stream = streams.pop(camera_id)
stream['stop_event'].set()
stream['thread'].join()
stream['cap'].release() stream['cap'].release()
logging.info(f"Unsubscribed from camera {camera_id}") logging.info(f"Unsubscribed from camera {camera_id}")
if camera_id in models and modelId in models[camera_id]: if camera_id in models and modelId in models[camera_id]:
@ -357,4 +360,4 @@ async def detect(websocket: WebSocket):
streams.clear() streams.clear()
with models_lock: with models_lock:
models.clear() models.clear()
logging.info("WebSocket connection closed") logging.info("WebSocket connection closed")