diff --git a/headmic.py b/headmic.py index 950b958..79df6c7 100644 --- a/headmic.py +++ b/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() # ============================================================================