Registry / testing / func-timeout

func-timeout

JSON →
library4.3.6pypypi✓ verified 35d 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.

testing
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.103.925 runs
installs and imports cleanly · install 0.0s · import 0.036s · 19.2MB
glibc
py 3.103.925 runs
installs and imports cleanly · install 2.4s · import 0.046s · 20MB
17MB installed
● package 17MB
Code
Verified usage

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

from func_timeout import func_timeout
from func_timeout import func_set_timeout
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 footguns
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.
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.
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.
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.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
14 hits · last 30 days
gptbot
4
ahrefsbot
4
bytedance
4
script
1
bingbot
1
Resources