skillfed

wrapt-timeout-decorator

The better timout decorator

wrapt-timeout-decorator v1.5.1 2.2M downloads/30d#3,216 on PyPI164
Permissive license MIT Active released

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-decorator

uv

uv add wrapt-timeout-decorator

poetry

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 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

Development Status :: 5 - Production/StableIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseNatural Language :: EnglishOperating System :: OS IndependentProgramming Language :: PythonTopic :: Software Development :: Libraries :: Python Modules

Tags

timeout decorator pythonfunction timeout limittimeout with subprocesssignal-based timeouttimeout for methods and classescross-platform timeouttimeout exception handler
timeout-enforcementcross-platformdecorator-pattern

More Python Modules packages