Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Weves committed Feb 12, 2025
1 parent 1ca509c commit 9bc7449
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions backend/tests/integration/common_utils/timeout.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import concurrent.futures
import multiprocessing
from collections.abc import Callable
from typing import TypeVar

T = TypeVar("T")


def run_with_timeout(task: Callable[[], T], timeout: int) -> T:
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(task)
# Use multiprocessing to prevent a thread from blocking the main thread
with multiprocessing.Pool(processes=1) as pool:
async_result = pool.apply_async(task)
try:
# Wait at most 5 seconds for the function to complete
result = future.result(timeout=timeout)
# Wait at most timeout seconds for the function to complete
result = async_result.get(timeout=timeout)
return result
except concurrent.futures.TimeoutError:
except multiprocessing.TimeoutError:
raise TimeoutError(f"Function timed out after {timeout} seconds")

0 comments on commit 9bc7449

Please sign in to comment.