`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-timeoutVerified import paths — ran on the pinned version, not inferred.
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.
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.
Ensure you are using version 4.1.0 or newer for improved traceback clarity and more robust thread cleanup.
Upgrade to version 4.3.0 or newer, which uses `functools.wraps` to preserve function metadata.
Always use the latest available version of `func-timeout` (currently 4.3.6) to ensure compatibility and leverage internal fixes for newer Python versions.
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!")
```Install the library using pip: `pip install func-timeout`
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.
No dependency data recorded yet.