Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: proper is_busy() function #158

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion _version.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from packaging import version

__version__ = "5.2.8"
__version__ = "5.2.10"


def version_major() -> int:
Expand Down
40 changes: 40 additions & 0 deletions module_wrapper_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from lmao.module_wrapper import STATUS_NOT_INITIALIZED, STATUS_IDLE, STATUS_BUSY, STATUS_FAILED
from lmao.module_wrapper import MODULES as LMAO_MODULES

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

# How long to wait for module no become not busy
_WAIT_FOR_IDLE_TIMEOUT = 10


class ModuleWrapperGlobal:
def __init__(
Expand Down Expand Up @@ -84,6 +88,9 @@ def __init__(

self.module = None

# PID for non-LMAO modules for is_busy() function
self._pid_value = multiprocessing.Value(c_int32, -1)

################
# LMAO modules #
################
Expand Down Expand Up @@ -178,6 +185,25 @@ def __init__(
elif name == "ms_copilot_designer":
self.module = MSCopilotDesignerModule(config, self.messages, self.users_handler)

def is_busy(self) -> bool:
"""
Returns:
bool: True if current module is busy, False if not
"""
# LMAO module is busy if it's status is not IDLE
if self.name.startswith("lmao_"):
with self._lmao_module_status.get_lock():
module_status = self._lmao_module_status.value
return module_status != STATUS_IDLE

# Other modules -> check for process_request() process
else:
with self._pid_value.get_lock():
pid = self._pid_value.value
if pid >= -1 and psutil.pid_exists(pid):
return True
return False

def process_request(self, request_response: request_response_container.RequestResponseContainer) -> None:
"""Processes request
This is called from separate queue process (non main)
Expand All @@ -188,6 +214,11 @@ def process_request(self, request_response: request_response_container.RequestRe
Raises:
Exception: process state / status or any other error
"""
# Set PID for is_busy() function
with self._pid_value.get_lock():
self._pid_value.value = multiprocessing.current_process().pid

# Extract user's language
user_id = request_response.user_id
lang_id = self.users_handler.get_key(user_id, "lang_id", "eng")

Expand Down Expand Up @@ -382,6 +413,15 @@ def delete_conversation(self, user_id: int) -> None:
Raises:
Exception: process state / status or any other error
"""
# Wait for module to become not busy or timeout
time_started = time.time()
while True:
if time.time() - time_started > _WAIT_FOR_IDLE_TIMEOUT:
raise Exception("Timeout waiting for module to become available. Please wait a bit and try again")
if not self.is_busy():
break
time.sleep(0.1)

# Redirect to LMAO process and wait
if self.name.startswith("lmao_"):
# Check status
Expand Down
14 changes: 2 additions & 12 deletions queue_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,9 @@ def _queue_processing_loop(self) -> None:
#################################################
# Check if we're not processing this request yet
if request_.processing_state == request_response_container.PROCESSING_STATE_IN_QUEUE:
# Check if requested module's process is busy (only 1 request to each module as a time)
module_is_busy = False
for request__ in queue_list:
if (
request__.module_name == request_.module_name
and request__.pid != 0
and psutil.pid_exists(request__.pid)
):
module_is_busy = True
break

# Ignore until module is no longer busy
if module_is_busy:
module = self.modules.get(request_.module_name)
if module is not None and module.is_busy():
continue

# Set initializing state
Expand Down
Loading