--- id: wrapt-timeout-decorator version: "1.5.1" license: MIT license_treatment: permissive maintenance: active --- # wrapt-timeout-decorator — The better timout decorator License: permissive · Maintenance: active · Downloads: 2.2M/mo ## What it is and what it does wrapt_timeout_decorator is a decorator library that enforces time limits on function execution in Python. It addresses the common problem of functions that may hang indefinitely by wrapping them with a timeout mechanism. Unlike simpler timeout decorators, this package handles edge cases correctly: it works with class methods, static methods, instance methods, and preserves traceback information for debugging in tools like PyCharm. The package offers two timeout strategies. On POSIX systems (Linux, macOS), it can use signals for elegant, efficient timeouts, but with caveats documented in the project. By default and on Windows, it uses subprocesses to enforce timeouts, which requires that decorated function arguments and return values be serializable via dill. The timeout value can be static, dynamically calculated, or adjusted via an optional eval function. It depends on wrapt, multiprocess, dill, psutil, and utility packages for subprocess management and test-environment detection. Use it for: - Wrap database queries or network calls that may hang, ensuring they fail fast with a timeout exception rather than blocking indefinitely. - Protect long-running file I/O or data processing operations in batch jobs, applying a timeout at the function level to prevent resource exhaustion. - Enforce time limits on third-party library calls where the library itself offers no timeout parameter, as a last resort after exhausting built-in options. - Handle Windows compatibility for timeout-protected code by automatically falling back to subprocess strategy when signals are unavailable. - Debug hanging functions in PyCharm by preserving full traceback information through the timeout mechanism. ## Worth the install? AI-flagged interpretation of the facts above — verify before relying. Applies timeout limits to Python functions and methods via decorator, with support for both signal-based (POSIX) and subprocess-based (cross-platform) strategies, preserving tracebacks and handling classes, methods, and static methods correctly. Yes, if you need a robust timeout decorator that handles methods and classes correctly and works cross-platform. The package is actively maintained, has no known vulnerabilities, and low install friction. However, reserve it as a last resort after checking whether the libraries you're calling already offer native timeout support; the subprocess overhead on Windows and repeated invocations can be significant. Ensure your decorated function arguments are pickleable. ## Install pip install wrapt-timeout-decorator uv add wrapt-timeout-decorator poetry add wrapt-timeout-decorator ## Installing wrapt-timeout-decorator Before you install: Low friction install with a pure-Python wheel. Maintenance is active with recent commits and stable production status. Six runtime dependencies (wrapt, multiprocess, dill, psutil, lib-detect-testenv, cli-exit-tools) are all standard libraries or well-maintained packages, posing no unusual risk. License in practice: MIT license is permissive with no restrictions on commercial or private use. You may use, modify, and distribute this package freely provided you include the license notice. Quickstart: from wrapt_timeout_decorator import timeout import time @timeout(5) def example(): time.sleep(10) if __name__ == '__main__': example() On Windows, subprocess strategy is the only option (signals unavailable); on POSIX systems, signals cannot be used within subthreads. Requires Python 3.8.0 or newer. Objects passed to decorated functions must be pickleable (dill is used for serialization). Verify before relying: - Whether the package handles timeout exceptions consistently across all Python versions it claims to support (3.8–3.12, PyPy 3.9–3.10). - Performance overhead of subprocess spawning on repeated decorator invocations in tight loops. - Compatibility with async/await and asyncio event loops beyond the mentioned FastAPI/Uvicorn considerations. ## Package facts - License: MIT (permissive) - Python support: supports_current - Install friction: low - Maintenance: active - Downloads: 2.2M/month (top 5,000 on PyPI) - Known vulnerabilities: none known ## Tags timeout decorator python, function timeout limit, timeout with subprocess, signal-based timeout, timeout for methods and classes, cross-platform timeout, timeout exception handler, timeout-enforcement, cross-platform, decorator-pattern [View on SkillFed](https://skillfed.io/packages/wrapt-timeout-decorator) · [View on PyPI](https://pypi.org/project/wrapt-timeout-decorator/)