28
28
from lmao .module_wrapper import STATUS_NOT_INITIALIZED , STATUS_IDLE , STATUS_BUSY , STATUS_FAILED
29
29
from lmao .module_wrapper import MODULES as LMAO_MODULES
30
30
31
+ import psutil
31
32
from google_ai_module import GoogleAIModule
32
33
from ms_copilot_module import MSCopilotModule
33
34
from ms_copilot_designer_module import MSCopilotDesignerModule
51
52
# Maximum time (in seconds) to wait for LMAO module to close before killing it's process
52
53
_LMAO_STOP_TIMEOUT = 10
53
54
55
+ # How long to wait for module no become not busy
56
+ _WAIT_FOR_IDLE_TIMEOUT = 10
57
+
54
58
55
59
class ModuleWrapperGlobal :
56
60
def __init__ (
@@ -84,6 +88,9 @@ def __init__(
84
88
85
89
self .module = None
86
90
91
+ # PID for non-LMAO modules for is_busy() function
92
+ self ._pid_value = multiprocessing .Value (c_int32 , - 1 )
93
+
87
94
################
88
95
# LMAO modules #
89
96
################
@@ -178,6 +185,25 @@ def __init__(
178
185
elif name == "ms_copilot_designer" :
179
186
self .module = MSCopilotDesignerModule (config , self .messages , self .users_handler )
180
187
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
+
181
207
def process_request (self , request_response : request_response_container .RequestResponseContainer ) -> None :
182
208
"""Processes request
183
209
This is called from separate queue process (non main)
@@ -188,6 +214,11 @@ def process_request(self, request_response: request_response_container.RequestRe
188
214
Raises:
189
215
Exception: process state / status or any other error
190
216
"""
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
191
222
user_id = request_response .user_id
192
223
lang_id = self .users_handler .get_key (user_id , "lang_id" , "eng" )
193
224
@@ -382,6 +413,15 @@ def delete_conversation(self, user_id: int) -> None:
382
413
Raises:
383
414
Exception: process state / status or any other error
384
415
"""
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
+
385
425
# Redirect to LMAO process and wait
386
426
if self .name .startswith ("lmao_" ):
387
427
# Check status
0 commit comments