mirror of
https://github.com/azaion/detections.git
synced 2026-04-22 11:06:32 +00:00
[AZ-180] Refactor detection event handling and improve SSE support
- Updated the detection image endpoint to require a channel ID for event streaming. - Introduced a new endpoint for streaming detection events, allowing clients to receive real-time updates. - Enhanced the internal buffering mechanism for detection events to manage multiple channels. - Refactored the inference module to support the new event handling structure. Made-with: Cursor
This commit is contained in:
+24
-14
@@ -1,6 +1,7 @@
|
||||
import json
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
@@ -24,29 +25,22 @@ def video_events(warm_engine, http_client, auth_headers):
|
||||
if not Path(_VIDEO).is_file():
|
||||
pytest.skip(f"missing fixture {_VIDEO}")
|
||||
|
||||
r = http_client.post(
|
||||
"/detect/video",
|
||||
data=_chunked_reader(_VIDEO),
|
||||
headers={
|
||||
**auth_headers,
|
||||
"X-Filename": "video_test01.mp4",
|
||||
"Content-Type": "application/octet-stream",
|
||||
},
|
||||
timeout=15,
|
||||
)
|
||||
assert r.status_code == 200
|
||||
media_id = r.json()["mediaId"]
|
||||
|
||||
channel_id = str(uuid.uuid4())
|
||||
collected: list[tuple[float, dict]] = []
|
||||
thread_exc: list[BaseException] = []
|
||||
done = threading.Event()
|
||||
connected = threading.Event()
|
||||
|
||||
def _listen():
|
||||
try:
|
||||
with http_client.get(
|
||||
f"/detect/{media_id}", stream=True, timeout=35, headers=auth_headers
|
||||
f"/detect/events/{channel_id}",
|
||||
stream=True,
|
||||
timeout=60,
|
||||
headers=auth_headers,
|
||||
) as resp:
|
||||
resp.raise_for_status()
|
||||
connected.set()
|
||||
sse = sseclient.SSEClient(resp)
|
||||
for event in sse.events():
|
||||
if not event.data or not str(event.data).strip():
|
||||
@@ -61,10 +55,26 @@ def video_events(warm_engine, http_client, auth_headers):
|
||||
except BaseException as e:
|
||||
thread_exc.append(e)
|
||||
finally:
|
||||
connected.set()
|
||||
done.set()
|
||||
|
||||
th = threading.Thread(target=_listen, daemon=True)
|
||||
th.start()
|
||||
connected.wait(timeout=5)
|
||||
|
||||
r = http_client.post(
|
||||
"/detect/video",
|
||||
data=_chunked_reader(_VIDEO),
|
||||
headers={
|
||||
**auth_headers,
|
||||
"X-Channel-Id": channel_id,
|
||||
"X-Filename": "video_test01.mp4",
|
||||
"Content-Type": "application/octet-stream",
|
||||
},
|
||||
timeout=15,
|
||||
)
|
||||
assert r.status_code == 202
|
||||
|
||||
assert done.wait(timeout=30)
|
||||
th.join(timeout=5)
|
||||
assert not thread_exc, thread_exc
|
||||
|
||||
Reference in New Issue
Block a user