diff options
author | 2023-03-13 14:18:56 -0400 | |
---|---|---|
committer | 2023-03-13 14:18:56 -0400 | |
commit | 0ef25529797d80ab8270123cc18b604c45bdf150 (patch) | |
tree | 1eda0832adbc036f114a49033342795029a124c8 | |
parent | Add SIGALRM based time limit (diff) |
Remove unused timed function
-rw-r--r-- | snekbox/utils/timed.py | 37 |
1 files changed, 4 insertions, 33 deletions
diff --git a/snekbox/utils/timed.py b/snekbox/utils/timed.py index bac299d..756b2bc 100644 --- a/snekbox/utils/timed.py +++ b/snekbox/utils/timed.py @@ -1,42 +1,13 @@ """Calling functions with time limits.""" -import multiprocessing import signal -from collections.abc import Callable, Generator, Iterable, Mapping +from collections.abc import Generator from contextlib import contextmanager -from typing import Any, TypeVar +from typing import TypeVar _T = TypeVar("_T") _V = TypeVar("_V") -__all__ = ("timed", "time_limit") - - -def timed( - func: Callable[[_T], _V], - args: Iterable = (), - kwds: Mapping[str, Any] | None = None, - timeout: float | None = None, -) -> _V: - """ - Call a function with a time limit. - - Args: - func: Function to call. - args: Arguments for function. - kwds: Keyword arguments for function. - timeout: Timeout limit in seconds. - - Raises: - TimeoutError: If the function call takes longer than `timeout` seconds. - """ - if kwds is None: - kwds = {} - with multiprocessing.Pool(1, maxtasksperchild=1) as pool: - result = pool.apply_async(func, args, kwds) - try: - return result.get(timeout) - except multiprocessing.TimeoutError as e: - raise TimeoutError(f"Call to {func.__name__} timed out after {timeout} seconds.") from e +__all__ = ("time_limit",) @contextmanager @@ -51,7 +22,7 @@ def time_limit(timeout: int | None = None) -> Generator[None, None, None]: TimeoutError: If the function call takes longer than `timeout` seconds. """ - def signal_handler(signum, frame): + def signal_handler(_signum, _frame): raise TimeoutError(f"time_limit call timed out after {timeout} seconds.") signal.signal(signal.SIGALRM, signal_handler) |