add ramdisk, load AI model to ramdisk and start recognition from it

rewrite zmq to DEALER and ROUTER
add GET_USER command to get CurrentUser from Python
all auth is on the python side
inference run and validate annotations on python
This commit is contained in:
Alex Bezdieniezhnykh
2025-01-29 17:45:26 +02:00
parent 82b3b526a7
commit 62623b7123
55 changed files with 945 additions and 895 deletions
+44 -56
View File
@@ -1,9 +1,7 @@
from queue import Queue
import zmq
import json
from threading import Thread, Event
from remote_command cimport RemoteCommand
cimport constants
cdef class RemoteCommandHandler:
def __init__(self, object on_command):
@@ -11,68 +9,58 @@ cdef class RemoteCommandHandler:
self._context = zmq.Context.instance()
self._shutdown_event = Event()
self._pull_socket = self._context.socket(zmq.PULL)
self._pull_socket.setsockopt(zmq.LINGER, 0)
self._pull_socket.bind("tcp://*:5127")
self._pull_thread = Thread(target=self._pull_loop, daemon=True)
self._router = self._context.socket(zmq.ROUTER)
self._router.setsockopt(zmq.LINGER, 0)
self._router.bind(f'tcp://*:{constants.ZMQ_PORT}')
self._push_queue = Queue()
self._dealer = self._context.socket(zmq.DEALER)
self._dealer.setsockopt(zmq.LINGER, 0)
self._dealer.bind("inproc://backend")
self._push_socket = self._context.socket(zmq.PUSH)
self._push_socket.setsockopt(zmq.LINGER, 0)
self._push_socket.bind("tcp://*:5128")
self._push_thread = Thread(target=self._push_loop, daemon=True)
self._proxy_thread = Thread(target=self._proxy_loop, daemon=True)
self._workers = []
for _ in range(4): # 4 worker threads
worker = Thread(target=self._worker_loop, daemon=True)
self._workers.append(worker)
cdef start(self):
self._pull_thread.start()
self._push_thread.start()
self._proxy_thread.start()
for worker in self._workers:
worker.start()
cdef _pull_loop(self):
while not self._shutdown_event.is_set():
print('wait for the command...')
message = self._pull_socket.recv()
cmd = RemoteCommand.from_msgpack(<bytes>message)
print(f'received: {cmd}')
self._on_command(cmd)
cdef _proxy_loop(self):
zmq.proxy(self._router, self._dealer)
cdef _push_loop(self):
cdef _worker_loop(self):
worker_socket = self._context.socket(zmq.DEALER)
worker_socket.setsockopt(zmq.LINGER, 0)
worker_socket.connect("inproc://backend")
poller = zmq.Poller()
poller.register(worker_socket, zmq.POLLIN)
print('started receiver loop...')
while not self._shutdown_event.is_set():
try:
response = self._push_queue.get(timeout=1) # Timeout to check shutdown flag
self._push_socket.send(response)
except:
continue
socks = dict(poller.poll(500))
if worker_socket in socks:
client_id, message = worker_socket.recv_multipart()
cmd = RemoteCommand.from_msgpack(<bytes> message)
cmd.client_id = client_id
print(f'Received [{cmd}] from the client {client_id}')
self._on_command(cmd)
except Exception as e:
print(f"Worker error: {e}")
import traceback
traceback.print_exc()
cdef send(self, bytes message_bytes):
print(f'about to send {len(message_bytes)}')
try:
self._push_queue.put(message_bytes)
except Exception as e:
print(e)
cdef send(self, bytes client_id, bytes data):
with self._context.socket(zmq.DEALER) as socket:
socket.connect("inproc://backend")
socket.send_multipart([client_id, data])
print(f'{len(data)} bytes was sent to client {client_id}')
cdef close(self):
self._shutdown_event.set()
self._pull_socket.close()
self._push_socket.close()
self._context.term()
cdef class QueueConfig:
cdef str host,
cdef int port, command_port
cdef str producer_user, producer_pw, consumer_user, consumer_pw
@staticmethod
cdef QueueConfig from_json(str json_string):
s = str(json_string).strip()
cdef dict config_dict = json.loads(s)["QueueConfig"]
cdef QueueConfig config = QueueConfig()
config.host = config_dict["Host"]
config.port = config_dict["Port"]
config.command_port = config_dict["CommandsPort"]
config.producer_user = config_dict["ProducerUsername"]
config.producer_pw = config_dict["ProducerPassword"]
config.consumer_user = config_dict["ConsumerUsername"]
config.consumer_pw = config_dict["ConsumerPassword"]
return config
self._router.close()
self._dealer.close()
self._context.term()