Skip to content

Commit ac0ad24

Browse files
committed
feat: proper is_busy() function
1 parent 2793e7a commit ac0ad24

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

module_wrapper_global.py

+40
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from lmao.module_wrapper import STATUS_NOT_INITIALIZED, STATUS_IDLE, STATUS_BUSY, STATUS_FAILED
2929
from lmao.module_wrapper import MODULES as LMAO_MODULES
3030

31+
import psutil
3132
from google_ai_module import GoogleAIModule
3233
from ms_copilot_module import MSCopilotModule
3334
from ms_copilot_designer_module import MSCopilotDesignerModule
@@ -51,6 +52,9 @@
5152
# Maximum time (in seconds) to wait for LMAO module to close before killing it's process
5253
_LMAO_STOP_TIMEOUT = 10
5354

55+
# How long to wait for module no become not busy
56+
_WAIT_FOR_IDLE_TIMEOUT = 10
57+
5458

5559
class ModuleWrapperGlobal:
5660
def __init__(
@@ -84,6 +88,9 @@ def __init__(
8488

8589
self.module = None
8690

91+
# PID for non-LMAO modules for is_busy() function
92+
self._pid_value = multiprocessing.Value(c_int32, -1)
93+
8794
################
8895
# LMAO modules #
8996
################
@@ -178,6 +185,25 @@ def __init__(
178185
elif name == "ms_copilot_designer":
179186
self.module = MSCopilotDesignerModule(config, self.messages, self.users_handler)
180187

188+
def is_busy(self) -> bool:
189+
"""
190+
Returns:
191+
bool: True if current module is busy, False if not
192+
"""
193+
# LMAO module is busy if it's status is not IDLE
194+
if self.name.startswith("lmao_"):
195+
with self._lmao_module_status.get_lock():
196+
module_status = self._lmao_module_status.value
197+
return module_status != STATUS_IDLE
198+
199+
# Other modules -> check for process_request() process
200+
else:
201+
with self._pid_value.get_lock():
202+
pid = self._pid_value.value
203+
if pid >= -1 and psutil.pid_exists(pid):
204+
return True
205+
return False
206+
181207
def process_request(self, request_response: request_response_container.RequestResponseContainer) -> None:
182208
"""Processes request
183209
This is called from separate queue process (non main)
@@ -188,6 +214,11 @@ def process_request(self, request_response: request_response_container.RequestRe
188214
Raises:
189215
Exception: process state / status or any other error
190216
"""
217+
# Set PID for is_busy() function
218+
with self._pid_value.get_lock():
219+
self._pid_value.value = multiprocessing.current_process().pid
220+
221+
# Extract user's language
191222
user_id = request_response.user_id
192223
lang_id = self.users_handler.get_key(user_id, "lang_id", "eng")
193224

@@ -382,6 +413,15 @@ def delete_conversation(self, user_id: int) -> None:
382413
Raises:
383414
Exception: process state / status or any other error
384415
"""
416+
# Wait for module to become not busy or timeout
417+
time_started = time.time()
418+
while True:
419+
if time.time() - time_started > _WAIT_FOR_IDLE_TIMEOUT:
420+
raise Exception("Timeout waiting for module to become available. Please wait a bit and try again")
421+
if not self.is_busy():
422+
break
423+
time.sleep(0.1)
424+
385425
# Redirect to LMAO process and wait
386426
if self.name.startswith("lmao_"):
387427
# Check status

queue_handler.py

+2-12
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,9 @@ def _queue_processing_loop(self) -> None:
151151
#################################################
152152
# Check if we're not processing this request yet
153153
if request_.processing_state == request_response_container.PROCESSING_STATE_IN_QUEUE:
154-
# Check if requested module's process is busy (only 1 request to each module as a time)
155-
module_is_busy = False
156-
for request__ in queue_list:
157-
if (
158-
request__.module_name == request_.module_name
159-
and request__.pid != 0
160-
and psutil.pid_exists(request__.pid)
161-
):
162-
module_is_busy = True
163-
break
164-
165154
# Ignore until module is no longer busy
166-
if module_is_busy:
155+
module = self.modules.get(request_.module_name)
156+
if module is not None and module.is_busy():
167157
continue
168158

169159
# Set initializing state

0 commit comments

Comments
 (0)