Files
test_dashboard/server/sse_py.py
T

33 lines
677 B
Python
Raw Normal View History

2026-05-26 14:36:34 -04:00
import json
import queue
import threading
_clients = set()
_clients_lock = threading.Lock()
def stream_events():
q = queue.Queue()
with _clients_lock:
_clients.add(q)
try:
yield 'data: {"type":"connected"}\n\n'
while True:
try:
payload = q.get(timeout=20)
yield f"data: {payload}\n\n"
except queue.Empty:
yield ": heartbeat\n\n"
finally:
with _clients_lock:
_clients.discard(q)
def broadcast(data):
payload = json.dumps(data)
with _clients_lock:
clients = list(_clients)
for q in clients:
q.put_nowait(payload)