Fix API hang — run gaze push in detached thread

Synchronous urllib.urlopen at 10Hz was starving uvicorn's event loop
via GIL contention. Now each push runs in its own daemon thread, and
skips if the previous push is still in flight (natural rate limiting).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Alex
2026-04-12 21:24:49 -05:00
parent c7b0be3319
commit 2bbbb6da2b

View File

@@ -463,17 +463,28 @@ def doa_track_loop():
time.sleep(interval) time.sleep(interval)
_gaze_push_thread = None
def _push_gaze(x: int, y: int): def _push_gaze(x: int, y: int):
"""Fire-and-forget gaze push to eye service. Uses urllib to avoid httpx connection overhead.""" """Fire-and-forget gaze push to eye service in a detached thread."""
try: global _gaze_push_thread
import urllib.request # Skip if previous push is still in flight
data = json.dumps({"x": x, "y": y}).encode() if _gaze_push_thread and _gaze_push_thread.is_alive():
req = urllib.request.Request( return
f"{EYE_SERVICE_URL}/gaze", data=data,
headers={"Content-Type": "application/json"}) def _send():
urllib.request.urlopen(req, timeout=0.3) try:
except Exception: import urllib.request
pass data = json.dumps({"x": x, "y": y}).encode()
req = urllib.request.Request(
f"{EYE_SERVICE_URL}/gaze", data=data,
headers={"Content-Type": "application/json"})
urllib.request.urlopen(req, timeout=0.5)
except Exception:
pass
_gaze_push_thread = threading.Thread(target=_send, daemon=True)
_gaze_push_thread.start()
# ============================================================================ # ============================================================================