perf(simulation): push current previews and streamline native sensor startup

This commit is contained in:
DCCONSTRUCTIONS
2026-10-09 10:09:59 +03:00
parent c4d4ea4cf8
commit 5e2e0aa4d7
18 changed files with 395 additions and 74 deletions
+10 -1
View File
@@ -5,7 +5,8 @@ from pathlib import Path
from typing import Literal
from fastapi import APIRouter, Header, HTTPException, Request, Response
from fastapi.responses import FileResponse
from fastapi.responses import FileResponse, StreamingResponse
from k1link.web.ai_preview_stream import preview_stream
from k1link.observatory.recorded_jobs import ObservatoryRecordedJobQueue
from k1link.simulation.ai_polygon.composition import compose, registry
@@ -244,6 +245,14 @@ def build_ai_polygon_router(
response.headers["Cache-Control"] = "no-store"
return invoke(runs.inference_preview, run_id)
@router.get("/runs/{run_id}/inference-preview/events")
async def inference_preview_events(run_id: str):
invoke(runs.get, run_id)
return StreamingResponse(
preview_stream(runs, run_id), media_type="text/event-stream",
headers={"Cache-Control": "no-store", "X-Accel-Buffering": "no"},
)
@router.post("/worker/runs/{run_id}/inference-preview")
def publish_inference_preview(
run_id: str,
+25
View File
@@ -0,0 +1,25 @@
"""Push the current preview on arrival; slow viewers skip obsolete frames."""
import asyncio
import json
async def preview_stream(runs, run_id):
queue, close = runs.preview_events.subscribe()
last_frame = -1
try:
yield "retry: 1000\n\n"
while True:
preview, active = runs.preview_snapshot(run_id)
if preview is not None and preview["frame_id"] > last_frame:
last_frame = preview["frame_id"]
yield "data: " + json.dumps(preview, separators=(",", ":")) + "\n\n"
if not active:
yield "event: end\ndata: null\n\n"
return
try:
await asyncio.wait_for(queue.get(), timeout=10)
except TimeoutError:
yield ": keepalive\n\n"
finally:
close()