Files
frigate/frigate/comms/config_updater.py
T

60 lines
1.8 KiB
Python
Raw Normal View History

"""Facilitates communication between processes."""
import multiprocessing as mp
2025-06-20 06:44:48 -06:00
from _pickle import UnpicklingError
from multiprocessing.synchronize import Event as MpEvent
2025-08-08 06:08:37 -06:00
from typing import Any
import zmq
SOCKET_PUB_SUB = "ipc:///tmp/cache/config"
class ConfigPublisher:
"""Publishes config changes to different processes."""
def __init__(self) -> None:
self.context = zmq.Context()
self.socket = self.context.socket(zmq.PUB)
self.socket.bind(SOCKET_PUB_SUB)
self.stop_event: MpEvent = mp.Event()
2025-05-13 16:27:20 +02:00
def publish(self, topic: str, payload: Any) -> None:
"""There is no communication back to the processes."""
self.socket.send_string(topic, flags=zmq.SNDMORE)
2024-07-24 09:37:29 -06:00
self.socket.send_pyobj(payload)
def stop(self) -> None:
self.stop_event.set()
2026-03-04 10:07:34 -06:00
self.socket.close(linger=0)
self.context.destroy(linger=0)
class ConfigSubscriber:
"""Simplifies receiving an updated config."""
2025-08-08 06:08:37 -06:00
def __init__(self, topic: str, exact: bool = False) -> None:
self.topic = topic
self.exact = exact
self.context = zmq.Context()
self.socket = self.context.socket(zmq.SUB)
self.socket.setsockopt_string(zmq.SUBSCRIBE, topic)
self.socket.connect(SOCKET_PUB_SUB)
2025-08-08 06:08:37 -06:00
def check_for_update(self) -> tuple[str, Any] | tuple[None, None]:
"""Returns updated config or None if no update."""
try:
topic = self.socket.recv_string(flags=zmq.NOBLOCK)
obj = self.socket.recv_pyobj()
if not self.exact or self.topic == topic:
return (topic, obj)
else:
return (None, None)
2025-06-20 06:44:48 -06:00
except (zmq.ZMQError, UnicodeDecodeError, UnpicklingError):
return (None, None)
def stop(self) -> None:
2026-03-04 10:07:34 -06:00
self.socket.close(linger=0)
self.context.destroy(linger=0)