5
5
import logging
6
6
import queue
7
7
import uuid
8
+ from enum import Enum
8
9
from .predictor import Predictor
9
10
10
11
WORKER_RESPONSE_TIMEOUT = 40
11
12
12
13
14
+ class Command (Enum ):
15
+ SET_MODEL = "SET_MODEL"
16
+ INFER = "INFER"
17
+ RESET = "RESET"
18
+
19
+
13
20
def _child_worker (request_queue , result_queue , model_name , force_cpu ):
14
21
signal .signal (signal .SIGINT , signal .SIG_IGN ) # Ignore Ctrl+C in child
15
22
logger = logging .getLogger (__name__ )
@@ -25,12 +32,12 @@ def _child_worker(request_queue, result_queue, model_name, force_cpu):
25
32
logger .debug ("Worker: Received EXIT command. Shutting down." )
26
33
break
27
34
28
- command = msg ["command" ]
35
+ command = Command ( msg ["command" ])
29
36
req_id = msg ["req_id" ]
30
37
payload = msg .get ("payload" , {})
31
- logger .debug (f"Worker: Received { command } with ID { req_id } " )
38
+ logger .debug (f"Worker: Received { command . value } with ID { req_id } " )
32
39
33
- if command == " SET_MODEL" :
40
+ if command == Command . SET_MODEL :
34
41
try :
35
42
predictor = Predictor (
36
43
model_name = payload ["model_name" ], force_cpu = payload ["force_cpu" ]
@@ -39,14 +46,14 @@ def _child_worker(request_queue, result_queue, model_name, force_cpu):
39
46
except Exception as e :
40
47
logger .exception ("Failed to set model." )
41
48
result_queue .put ((req_id , {"status" : "ERROR" , "message" : str (e )}))
42
- elif command == " INFER" :
49
+ elif command == Command . INFER :
43
50
try :
44
51
predictions = predictor .eval (payload ["images" ])
45
52
result_queue .put ((req_id , {"status" : "OK" , "result" : predictions }))
46
53
except Exception as e :
47
54
logger .exception ("Inference failed." )
48
55
result_queue .put ((req_id , {"status" : "ERROR" , "message" : str (e )}))
49
- elif command == " RESET" :
56
+ elif command == Command . RESET :
50
57
try :
51
58
predictor .reset ()
52
59
result_queue .put ((req_id , {"status" : "OK" }))
@@ -115,7 +122,7 @@ def set_model(self, model_name, force_cpu=False):
115
122
req_id = str (uuid .uuid4 ())
116
123
self ._request_queue .put (
117
124
{
118
- "command" : " SET_MODEL" ,
125
+ "command" : Command . SET_MODEL . value ,
119
126
"req_id" : req_id ,
120
127
"payload" : {
121
128
"model_name" : self .model_name ,
@@ -130,7 +137,11 @@ async def infer(self, images):
130
137
return {}
131
138
with self ._lock :
132
139
req_id = str (uuid .uuid4 ())
133
- new_req = {"command" : "INFER" , "req_id" : req_id , "payload" : {"images" : images }}
140
+ new_req = {
141
+ "command" : Command .INFER .value ,
142
+ "req_id" : req_id ,
143
+ "payload" : {"images" : images },
144
+ }
134
145
self ._request_queue .put (new_req )
135
146
136
147
resp = await self ._wait_for_response_async (req_id )
@@ -139,7 +150,7 @@ async def infer(self, images):
139
150
def reset (self ):
140
151
with self ._lock :
141
152
req_id = str (uuid .uuid4 ())
142
- self ._request_queue .put ({"command" : " RESET" , "req_id" : req_id })
153
+ self ._request_queue .put ({"command" : Command . RESET . value , "req_id" : req_id })
143
154
return self ._wait_for_response (req_id )
144
155
145
156
def shutdown (self ):
0 commit comments