wrapt-timeout-decorator
The better timout decorator
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 on this page — 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
wrapt-timeout-decorator on PyPI
pip
pip install wrapt-timeout-decoratoruv
uv add wrapt-timeout-decoratorpoetry
poetry add wrapt-timeout-decoratorInstalling 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 the current Python release (>=3.8.0) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 6 — cli-exit-tools, lib-detect-testenv, multiprocess, psutil, wrapt, dill |
| Maintenance | actively maintained — 898 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 2,198,074/month — #3,216 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: wrapt_timeout_decorator-1.5.1-py3-none-any.whl
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
timeout-decoratorA decorator that enforces time limits on…
permissive · top 5,000 on PyPI
interruptingcowInterrupts long-running Python code by raising…
permissive · top 15,000 on PyPI
cli-exit-toolsHandles proper exit procedures for CLI…
permissive · top 5,000 on PyPI
retry-decoratorProvides a decorator to automatically retry a…
permissive · top 15,000 on PyPI
stopitProvides context managers and decorators to…
permissive · top 15,000 on PyPI
pytest-timeoutAborts pytest tests that exceed a specified…
permissive · top 1,000 on PyPI
EasyProcessEasyProcess wraps Python's subprocess module to…
permissive · top 5,000 on PyPI
func-timeoutRuns any Python function with a specified…
copyleft · top 5,000 on PyPI
wraptwrapt provides a Python module for building…
permissive · top 100 on PyPI
pytest-timeoutsA pytest plugin that enforces separate timeout…
permissive · top 5,000 on PyPI