Skip to content

Commit

Permalink
Merge pull request #5 from javierganan99/develop
Browse files Browse the repository at this point in the history
Modified run_with_timeout to use thread instead of process
  • Loading branch information
javierganan99 authored Mar 9, 2024
2 parents b94c964 + e2bf4ef commit 5d2c670
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
59 changes: 30 additions & 29 deletions LLMCode/utils/completion.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
import multiprocessing
import threading
import openai
import LLMCode.cfg.completion_params as completion_params
from . import ANSI_CODE
from .logger import LOGGER


def run_with_timeout(target_function, args=None, timeout=30):
pool = multiprocessing.Pool(processes=1)
if args is None:
result = pool.apply_async(target_function)
else:
result = pool.apply_async(target_function, args)
try:
result_value = result.get(timeout=timeout)
return result_value
except Exception as e:
if isinstance(e, multiprocessing.TimeoutError):
LOGGER.info(
"%s\r⚠ The completion could not be done. %s response lasted more than %s seconds, which is the limit.",
ANSI_CODE["yellow"],
target_function,
timeout,
)
else:
LOGGER.info(
"%s\r⚠ The completion could not be done. %s raised the exception %s",
ANSI_CODE["yellow"],
target_function,
e,
)
pool.terminate()
def run_with_timeout(target_function, args=(), kwargs={}, timeout=30):
result = [None] # A mutable container to store the function result
exception = [None] # A container to capture exceptions

def target_wrapper():
try:
result[0] = target_function(*args, **kwargs)
except Exception as e:
exception[0] = e

thread = threading.Thread(target=target_wrapper)
thread.start()
thread.join(timeout)
if thread.is_alive():
LOGGER.info(
"%s⚠ The completion could not be done. %s response lasted more than %s seconds, which is the limit.",
ANSI_CODE["yellow"],
target_function.__name__,
timeout,
)
return None
if exception[0]:
LOGGER.info(
"%s⚠ The completion could not be done. %s raised the exception %s",
ANSI_CODE["yellow"],
target_function.__name__,
exception[0],
)
return None
finally:
pool.close()
pool.join()
return result[0]


# Completion using openai API
Expand Down
2 changes: 1 addition & 1 deletion LLMCode/utils/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,5 @@ def add_msg(msg, where, element, script_content):
script_content,
)
pass
with open(script, "w") as python_file:
with open(script, "w", encoding="utf-8") as python_file:
python_file.write(script_content)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
openai
openai==0.28

0 comments on commit 5d2c670

Please sign in to comment.