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:
31
headmic.py
31
headmic.py
@@ -463,17 +463,28 @@ def doa_track_loop():
|
||||
time.sleep(interval)
|
||||
|
||||
|
||||
_gaze_push_thread = None
|
||||
|
||||
def _push_gaze(x: int, y: int):
|
||||
"""Fire-and-forget gaze push to eye service. Uses urllib to avoid httpx connection overhead."""
|
||||
try:
|
||||
import urllib.request
|
||||
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.3)
|
||||
except Exception:
|
||||
pass
|
||||
"""Fire-and-forget gaze push to eye service in a detached thread."""
|
||||
global _gaze_push_thread
|
||||
# Skip if previous push is still in flight
|
||||
if _gaze_push_thread and _gaze_push_thread.is_alive():
|
||||
return
|
||||
|
||||
def _send():
|
||||
try:
|
||||
import urllib.request
|
||||
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()
|
||||
|
||||
|
||||
# ============================================================================
|
||||
|
||||
Reference in New Issue
Block a user