Fix ZMQ race condition with events (#13198)

This commit is contained in:
Nicolas Mowen 2024-08-19 15:23:38 -06:00
parent a77436eec3
commit 01fa1777ac

View File

@ -1,5 +1,6 @@
"""Facilitates communication over zmq proxy.""" """Facilitates communication over zmq proxy."""
import json
import threading import threading
from typing import Optional from typing import Optional
@ -58,8 +59,7 @@ class Publisher:
def publish(self, payload: any, sub_topic: str = "") -> None: def publish(self, payload: any, sub_topic: str = "") -> None:
"""Publish message.""" """Publish message."""
self.socket.send_string(f"{self.topic}{sub_topic}", flags=zmq.SNDMORE) self.socket.send_string(f"{self.topic}{sub_topic} {json.dumps(payload)}")
self.socket.send_json(payload)
def stop(self) -> None: def stop(self) -> None:
self.socket.close() self.socket.close()
@ -84,9 +84,8 @@ class Subscriber:
has_update, _, _ = zmq.select([self.socket], [], [], timeout) has_update, _, _ = zmq.select([self.socket], [], [], timeout)
if has_update: if has_update:
topic = self.socket.recv_string(flags=zmq.NOBLOCK) parts = self.socket.recv_string(flags=zmq.NOBLOCK).split(maxsplit=1)
payload = self.socket.recv_json() return self._return_object(parts[0], json.loads(parts[1]))
return self._return_object(topic, payload)
except zmq.ZMQError: except zmq.ZMQError:
pass pass