Registry / testing / func-timeout

func-timeout

JSON →
library4.3.5pypypi✓ verified 31d ago

`func-timeout` is a Python module that enables setting timeouts for arbitrary function calls, preventing them from running indefinitely. It achieves this by executing the function in a separate thread and forcefully terminating it if the specified duration is exceeded. The latest version is 4.3.6, with a historically sporadic release cadence, recently updated to support newer Python versions.

pip install func-timeout
INSTALL
IMPORT
SIG · FUNC-TIMEOUT
F
func-timeout
testingpythonv4.3.5
Install
2.4s avg
Import
39ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v4.3.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.036s · 19.2MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 2.4s · import 0.042s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

func_timeout
✓ from func_timeout import func_timeout
func_set_timeout
✓ from func_timeout import func_set_timeout
FunctionTimedOut
✓ from func_timeout import FunctionTimedOut

This quickstart demonstrates how to use both the `func_timeout` function and the `func_set_timeout` decorator to apply timeouts to Python functions. It shows how `FunctionTimedOut` is raised when the timeout is exceeded.

import time from func_timeout import func_timeout, FunctionTimedOut, func_set_timeout def long_running_function(duration): print(f"Starting long_running_function for {duration} seconds...") time.sleep(duration) print("long_running_function completed.") return "Done" # Example 1: Using func_timeout for a direct call try: print("\n--- Using func_timeout ---") result = func_timeout(1, long_running_function, args=(5,)) print(f"Result: {result}") except FunctionTimedOut: print("Function timed out after 1 second!") except Exception as e: print(f"An unexpected error occurred: {e}") # Example 2: Using func_set_timeout decorator @func_set_timeout(1) def decorated_function(duration): print(f"Starting decorated_function for {duration} seconds...") time.sleep(duration) print("decorated_function completed.") return "Decorated Done" try: print("\n--- Using func_set_timeout decorator ---") result = decorated_function(5) print(f"Result from decorator: {result}") except FunctionTimedOut: print("Decorated function timed out after 1 second!") except Exception as e: print(f"An unexpected error occurred: {e}")
Debug
Known issues
gotchaThread Termination Safety: `func-timeout` achieves timeouts by forcefully terminating a separate thread. This is inherently unsafe in Python, as threads cannot be reliably or gracefully stopped. This can lead to resource leaks (e.g., open files, network connections), corrupted shared state, or deadlocks if the timed-out function was holding locks or critical resources.
fix
Design functions to be interruptible, use cooperative cancellation (e.g., checking a flag periodically), or be acutely aware of potential side effects and design your application to tolerate them. Avoid `func-timeout` for functions performing critical operations or managing external resources without explicit cleanup.
affects: All versions
gotchaUnpredictable Tracebacks and Cleanup: In versions prior to 4.1.0, tracebacks might have been less clear, and explicit thread cleanup might not have occurred reliably, potentially leading to zombie threads or resource issues. Version 4.0.0 also fixed issues where tracebacks could be printed to stderr outside the normal exception handling.
fix
Ensure you are using version 4.1.0 or newer for improved traceback clarity and more robust thread cleanup.
affects: < 4.1.0 (especially < 4.0.0 for traceback issues)
gotchaDecorator Metadata Loss (pre-4.3.0): When using the `@func_set_timeout` decorator in versions prior to 4.3.0, the decorated function might lose its original metadata (e.g., `__name__`, `__doc__`). This can affect introspection tools or other decorators in a chain.
fix
Upgrade to version 4.3.0 or newer, which uses `functools.wraps` to preserve function metadata.
affects: < 4.3.0
gotchaPython Version Compatibility: Older versions of `func-timeout` might use deprecated threading API calls (e.g., `isA` instead of `is_alive` in versions before 4.3.4) or have less robust support for newer Python interpreters.
fix
Always use the latest available version of `func-timeout` (currently 4.3.6) to ensure compatibility and leverage internal fixes for newer Python versions.
affects: < 4.3.4 (for deprecated `isA`), potentially older versions for broader Python 3.x support
Errors
Common errors & fixes
func_timeout.exceptions.FunctionTimedOut
This exception is raised when the function being monitored by `func-timeout` exceeds the specified timeout duration before completing its execution.
fix
Wrap the call to the timed-out function in a `try...except func_timeout.FunctionTimedOut` block to gracefully handle the timeout.

Example:
```python
from func_timeout import func_timeout, FunctionTimedOut
import time

def long_running_function():
    time.sleep(5) # Simulates a long-running task
    return "Done"

try:
    result = func_timeout(2, long_running_function)
    print(result)
except FunctionTimedOut:
    print("Function timed out after 2 seconds!")
```
ModuleNotFoundError: No module named 'func-timeout'
The `func-timeout` library has not been installed in your Python environment or the environment where your script is being run.
fix
Install the library using pip: `pip install func-timeout`
AttributeError: module 'signal' has no attribute 'SIGALRM'
This error typically occurs when `func-timeout` (or related timeout libraries that rely on `signal` module for timeouts) is used on Windows operating systems, as `SIGALRM` is a Unix-specific signal and is not available on Windows.
fix
The `func-timeout` library handles timeouts using threads, which should work cross-platform without relying on `SIGALRM`. If you encounter this error, ensure you are using `func_timeout.func_timeout` or `@func_timeout.func_set_timeout` and that the library is correctly installed. This error is more commonly associated with other timeout libraries (like `timeout-decorator`) that default to `signal` for timeouts. `func-timeout` is designed to be cross-platform by using threads.
Upgrade
Version history
4.3.5latest on PyPI · released Aug 19, 2019
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
12
Resources
func-timeout — pip install func-timeout · libregistry