perf(simulation): push current previews and streamline native sensor startup
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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()
|
||||
Reference in New Issue
Block a user