2024-02-15 01:24:36 +01:00
|
|
|
"""Facilitates communication between processes."""
|
|
|
|
|
2023-07-14 02:52:33 +02:00
|
|
|
import multiprocessing as mp
|
2024-02-15 01:24:36 +01:00
|
|
|
import os
|
2023-07-14 02:52:33 +02:00
|
|
|
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-15 01:24:36 +01:00
|
|
|
from frigate.const import PORT_INTER_PROCESS_COMM
|
2023-07-14 02:52:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
class InterProcessCommunicator(Communicator):
|
2024-02-15 01:24:36 +01:00
|
|
|
def __init__(self) -> None:
|
|
|
|
INTER_PROCESS_COMM_PORT = (
|
|
|
|
os.environ.get("INTER_PROCESS_COMM_PORT") or PORT_INTER_PROCESS_COMM
|
|
|
|
)
|
|
|
|
self.context = zmq.Context()
|
|
|
|
self.socket = self.context.socket(zmq.REP)
|
|
|
|
self.socket.bind(f"tcp://127.0.0.1:{INTER_PROCESS_COMM_PORT}")
|
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-15 01:24:36 +01:00
|
|
|
while not self.stop_event.wait(0.5):
|
|
|
|
while True: # load all messages that are queued
|
|
|
|
try:
|
|
|
|
(topic, value) = self.socket.recv_pyobj(flags=zmq.NOBLOCK)
|
|
|
|
|
|
|
|
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:
|
|
|
|
self.socket.send_pyobj(response)
|
|
|
|
else:
|
|
|
|
self.socket.send_pyobj([])
|
|
|
|
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:
|
|
|
|
port = os.environ.get("INTER_PROCESS_COMM_PORT") or PORT_INTER_PROCESS_COMM
|
|
|
|
self.context = zmq.Context()
|
|
|
|
self.socket = self.context.socket(zmq.REQ)
|
|
|
|
self.socket.connect(f"tcp://127.0.0.1:{port}")
|
|
|
|
|
|
|
|
def send_data(self, topic: str, data: any) -> any:
|
|
|
|
"""Sends data and then waits for reply."""
|
|
|
|
self.socket.send_pyobj((topic, data))
|
|
|
|
return self.socket.recv_pyobj()
|
|
|
|
|
|
|
|
def stop(self) -> None:
|
|
|
|
self.socket.close()
|
|
|
|
self.context.destroy()
|