timeoutcontext

raw JSON →
2.0.0 verified Sat May 09 auth: no python

A signal-based timeout context manager for Python. Current version 2.0.0. Uses OS signals to interrupt long-running operations. Updated to require Python >=3.10. Release cadence is low (major versions separated by years).

pip install timeoutcontext
error AttributeError: module 'signal' has no attribute 'SIGALRM'
cause Running on Windows where SIGALRM is not available.
fix
timeoutcontext will silently not timeout on Windows. Use on Unix or handle unsupported OS explicitly.
error NameError: name 'Timeout' is not defined
cause Version 2.0.0 renamed the exception to TimeoutError.
fix
Import TimeoutError from timeoutcontext: from timeoutcontext import TimeoutError or catch the built-in TimeoutError directly (Python 3.11+).
breaking Version 2.0.0 dropped support for Python <3.10. The exception changed from `Timeout` (custom) to the built-in `TimeoutError`.
fix Use `except TimeoutError` instead of `except Timeout`. Update Python requirement to >=3.10.
gotcha The context manager uses SIGALRM on Unix. On Windows, signal-based timeouts are not supported; it will gracefully fall back to no timeout. Do not rely on timeout behavior on Windows.
fix Use on Unix/Linux/macOS for correct behavior. On Windows, consider alternative libraries like `stopit` or `threading.Timer`.

Basic usage: wrap long-running operations in a `with timeout(seconds)` block. Raises TimeoutError if time exceeded.

from timeoutcontext import timeout
import time
try:
    with timeout(2):
        time.sleep(5)
        print('Completed')
except TimeoutError:
    print('Operation timed out')