2024-02-15 01:24:36 +01:00
|
|
|
"""Facilitates communication between processes."""
|
|
|
|
|
2023-07-14 02:52:33 +02:00
|
|
|
import multiprocessing as mp
|
|
|
|
import threading
|
|
|
|
from multiprocessing.synchronize import Event as MpEvent
|
|
|
|
from typing import Callable
|
|
|
|
|
2024-02-15 01:24:36 +01:00
|
|
|
import zmq
|
|
|
|
|
2023-07-14 02:52:33 +02:00
|
|
|
from frigate.comms.dispatcher import Communicator
|
2024-02-19 14:26:59 +01:00
|
|
|
|
|
|
|
SOCKET_REP_REQ = "ipc:///tmp/cache/comms"
|
2023-07-14 02:52:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
class InterProcessCommunicator(Communicator):
|
2024-02-15 01:24:36 +01:00
|
|
|
def __init__(self) -> None:
|
|
|
|
self.context = zmq.Context()
|
|
|
|
self.socket = self.context.socket(zmq.REP)
|
2024-02-19 14:26:59 +01:00
|
|
|
self.socket.bind(SOCKET_REP_REQ)
|
2023-07-14 02:52:33 +02:00
|
|
|
self.stop_event: MpEvent = mp.Event()
|
|
|
|
|
|
|
|
def publish(self, topic: str, payload: str, retain: bool) -> None:
|
|
|
|
"""There is no communication back to the processes."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def subscribe(self, receiver: Callable) -> None:
|
|
|
|
self._dispatcher = receiver
|
|
|
|
self.reader_thread = threading.Thread(target=self.read)
|
|
|
|
self.reader_thread.start()
|
|
|
|
|
|
|
|
def read(self) -> None:
|
2024-02-19 14:26:59 +01:00
|
|
|
while not self.stop_event.is_set():
|
2024-02-15 01:24:36 +01:00
|
|
|
while True: # load all messages that are queued
|
2024-02-19 14:26:59 +01:00
|
|
|
has_message, _, _ = zmq.select([self.socket], [], [], 1)
|
|
|
|
|
|
|
|
if not has_message:
|
|
|
|
break
|
|
|
|
|
2024-02-15 01:24:36 +01:00
|
|
|
try:
|
2024-07-24 16:58:23 +02:00
|
|
|
(topic, value) = self.socket.recv_json(flags=zmq.NOBLOCK)
|
2024-02-15 01:24:36 +01:00
|
|
|
|
|
|
|
response = self._dispatcher(topic, value)
|
2023-07-14 02:52:33 +02:00
|
|
|
|
2024-02-15 01:24:36 +01:00
|
|
|
if response is not None:
|
2024-07-24 16:58:23 +02:00
|
|
|
self.socket.send_json(response)
|
2024-02-15 01:24:36 +01:00
|
|
|
else:
|
2024-07-24 16:58:23 +02:00
|
|
|
self.socket.send_json([])
|
2024-02-15 01:24:36 +01:00
|
|
|
except zmq.ZMQError:
|
|
|
|
break
|
2023-07-14 02:52:33 +02:00
|
|
|
|
|
|
|
def stop(self) -> None:
|
|
|
|
self.stop_event.set()
|
|
|
|
self.reader_thread.join()
|
2024-02-15 01:24:36 +01:00
|
|
|
self.socket.close()
|
|
|
|
self.context.destroy()
|
|
|
|
|
|
|
|
|
|
|
|
class InterProcessRequestor:
|
|
|
|
"""Simplifies sending data to InterProcessCommunicator and getting a reply."""
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.context = zmq.Context()
|
|
|
|
self.socket = self.context.socket(zmq.REQ)
|
2024-02-19 14:26:59 +01:00
|
|
|
self.socket.connect(SOCKET_REP_REQ)
|
2024-02-15 01:24:36 +01:00
|
|
|
|
|
|
|
def send_data(self, topic: str, data: any) -> any:
|
|
|
|
"""Sends data and then waits for reply."""
|
2024-07-24 16:58:23 +02:00
|
|
|
self.socket.send_json((topic, data))
|
|
|
|
return self.socket.recv_json()
|
2024-02-15 01:24:36 +01:00
|
|
|
|
|
|
|
def stop(self) -> None:
|
|
|
|
self.socket.close()
|
|
|
|
self.context.destroy()
|