From dae0f89b674df52b29c0f7d4b8ef45c9906d0e0a Mon Sep 17 00:00:00 2001 From: phi Date: Thu, 29 Feb 2024 19:06:48 +0900 Subject: [PATCH] fix: add docs --- src/timeout_executor/const.py | 2 +- src/timeout_executor/executor.py | 46 ++++++++++++++++++++++++++++++++ src/timeout_executor/result.py | 4 +++ src/timeout_executor/serde.py | 4 +++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/timeout_executor/const.py b/src/timeout_executor/const.py index 8237563..05d17fc 100644 --- a/src/timeout_executor/const.py +++ b/src/timeout_executor/const.py @@ -1,6 +1,6 @@ from __future__ import annotations -__all__ = ["TIMEOUT_EXECUTOR_INPUT_FILE"] +__all__ = ["TIMEOUT_EXECUTOR_INPUT_FILE", "SUBPROCESS_COMMAND"] TIMEOUT_EXECUTOR_INPUT_FILE = "_TIMEOUT_EXECUTOR_INPUT_FILE" SUBPROCESS_COMMAND = ( "from timeout_executor.subprocess import run_in_subprocess;run_in_subprocess()" diff --git a/src/timeout_executor/executor.py b/src/timeout_executor/executor.py index 845d52a..de5d3ae 100644 --- a/src/timeout_executor/executor.py +++ b/src/timeout_executor/executor.py @@ -105,6 +105,15 @@ def apply_func( def apply_func( timeout: float, func: Callable[P2, Any], *args: P2.args, **kwargs: P2.kwargs ) -> AsyncResult[Any]: + """run function with deadline + + Args: + timeout: deadline + func: func(sync or async) + + Returns: + async result container + """ executor = _Executor(timeout, func) return executor.apply(*args, **kwargs) @@ -127,11 +136,22 @@ async def delay_func( async def delay_func( timeout: float, func: Callable[P2, Any], *args: P2.args, **kwargs: P2.kwargs ) -> AsyncResult[Any]: + """run function with deadline + + Args: + timeout: deadline + func: func(sync or async) + + Returns: + async result container + """ executor = _Executor(timeout, func) return await executor.delay(*args, **kwargs) class TimeoutExecutor: + """timeout executor""" + def __init__(self, timeout: float) -> None: self._timeout = timeout @@ -152,6 +172,14 @@ def apply( def apply( self, func: Callable[P, Any], *args: P.args, **kwargs: P.kwargs ) -> AsyncResult[Any]: + """run function with deadline + + Args: + func: func(sync or async) + + Returns: + async result container + """ return apply_func(self._timeout, func, *args, **kwargs) @overload @@ -168,6 +196,14 @@ async def delay( async def delay( self, func: Callable[P, Any], *args: P.args, **kwargs: P.kwargs ) -> AsyncResult[Any]: + """run function with deadline + + Args: + func: func(sync or async) + + Returns: + async result container + """ return await delay_func(self._timeout, func, *args, **kwargs) @overload @@ -184,6 +220,16 @@ async def apply_async( async def apply_async( self, func: Callable[P, Any], *args: P.args, **kwargs: P.kwargs ) -> AsyncResult[Any]: + """run function with deadline. + + alias of `delay` + + Args: + func: func(sync or async) + + Returns: + async result container + """ return await self.delay(func, *args, **kwargs) diff --git a/src/timeout_executor/result.py b/src/timeout_executor/result.py index 15e55e5..c8ccdd0 100644 --- a/src/timeout_executor/result.py +++ b/src/timeout_executor/result.py @@ -22,6 +22,8 @@ class AsyncResult(Generic[T]): + """async result container""" + _result: Any def __init__( @@ -44,10 +46,12 @@ def __init__( self._input = input_file def result(self, timeout: float | None = None) -> T: + """get value sync method""" future = async_to_sync(self.delay) return future(timeout) async def delay(self, timeout: float | None = None) -> T: + """get value async method""" try: return await self._delay(timeout) finally: diff --git a/src/timeout_executor/serde.py b/src/timeout_executor/serde.py index d681cfc..cd42ce6 100644 --- a/src/timeout_executor/serde.py +++ b/src/timeout_executor/serde.py @@ -31,6 +31,7 @@ def serialize_traceback(traceback: TracebackType) -> tuple[Any, ...]: def serialize_error(error: Exception) -> SerializedError: + """serialize exception""" exception = pickle_exception(error)[1:] exception_args, exception = exception[0], exception[1:] @@ -62,6 +63,7 @@ def serialize_error(error: Exception) -> SerializedError: def deserialize_error(error: SerializedError) -> Exception: + """deserialize exception""" arg_exception: deque[Any] = deque(error.arg_exception) arg_tracebacks: deque[tuple[int, tuple[Any, ...]]] = deque(error.arg_tracebacks) @@ -79,6 +81,7 @@ def deserialize_error(error: SerializedError) -> Exception: def dumps_error(error: Exception | SerializedError) -> bytes: + """serialize exception as bytes""" if not isinstance(error, SerializedError): error = serialize_error(error) @@ -86,6 +89,7 @@ def dumps_error(error: Exception | SerializedError) -> bytes: def loads_error(error: bytes | SerializedError) -> Exception: + """deserialize exception from bytes""" if isinstance(error, bytes): error = cloudpickle.loads(error) if not isinstance(error, SerializedError):