feat: add export endpoint with WebSocket progress

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-16 13:49:16 +02:00
parent dfba88a601
commit 1f6906c946
2 changed files with 167 additions and 1 deletions
+37
View File
@@ -0,0 +1,37 @@
import asyncio
import json
from fastapi import WebSocket, WebSocketDisconnect
_connections: list[WebSocket] = []
async def connect(ws: WebSocket):
await ws.accept()
_connections.append(ws)
try:
while True:
await ws.receive_text() # keep alive
except WebSocketDisconnect:
_connections.remove(ws)
def broadcast(msg: dict):
"""Send a message to all connected WebSocket clients.
Called from sync code (export callbacks), so we schedule the coroutine
on each connection's event loop.
"""
data = json.dumps(msg)
stale = []
for ws in _connections:
try:
loop = asyncio.get_event_loop()
if loop.is_running():
asyncio.run_coroutine_threadsafe(ws.send_text(data), loop)
else:
loop.run_until_complete(ws.send_text(data))
except Exception:
stale.append(ws)
for ws in stale:
_connections.remove(ws)