make cython app exit correctly

This commit is contained in:
Alex Bezdieniezhnykh
2025-02-11 20:40:49 +02:00
parent 9973a16ada
commit 43cae0d03c
9 changed files with 87 additions and 65 deletions
+12 -5
View File
@@ -1,6 +1,8 @@
import queue
import traceback
from queue import Queue
cimport constants
from threading import Thread
from api_client cimport ApiClient
from annotation cimport Annotation
@@ -20,14 +22,14 @@ cdef class ParsedArguments:
cdef class CommandProcessor:
cdef ApiClient api_client
cdef RemoteCommandHandler remote_handler
cdef object command_queue
cdef object inference_queue
cdef bint running
cdef Inference inference
def __init__(self, args: ParsedArguments):
self.api_client = ApiClient(args.email, args.password, args.folder)
self.remote_handler = RemoteCommandHandler(self.on_command)
self.command_queue = Queue(maxsize=constants.QUEUE_MAXSIZE)
self.inference_queue = Queue(maxsize=constants.QUEUE_MAXSIZE)
self.remote_handler.start()
self.running = True
model = self.api_client.load_ai_model()
@@ -36,11 +38,14 @@ cdef class CommandProcessor:
def start(self):
while self.running:
try:
command = self.command_queue.get()
command = self.inference_queue.get(timeout=0.5)
self.inference.run_inference(command)
self.remote_handler.send(command.client_id, <bytes>'DONE'.encode('utf-8'))
except queue.Empty:
continue
except Exception as e:
traceback.print_exc()
print('EXIT!')
cdef on_command(self, RemoteCommand command):
try:
@@ -50,11 +55,12 @@ cdef class CommandProcessor:
response = self.api_client.load_bytes(command.filename)
self.remote_handler.send(command.client_id, response)
elif command.command_type == CommandType.INFERENCE:
self.command_queue.put(command)
self.inference_queue.put(command)
elif command.command_type == CommandType.STOP_INFERENCE:
self.inference.stop()
elif command.command_type == CommandType.EXIT:
self.stop()
t = Thread(target=self.stop) # non-block worker:
t.start()
else:
pass
except Exception as e:
@@ -68,5 +74,6 @@ cdef class CommandProcessor:
self.remote_handler.send(cmd.client_id, data)
def stop(self):
self.inference.stop()
self.remote_handler.stop()
self.running = False