Timeout Decorator
A versatile timeout decorator for both synchronous and asynchronous functions in Python.
Features
- Works with both synchronous and asynchronous functions
- Customizable timeout duration
- Optional callback function on timeout
- Support for custom exception on timeout
- Colored logging
Installation
pip install timeout-decorator
Parameters
timeout: The maximum execution time in seconds.
callback (optional): A function to be called if the timeout occurs.
custom_error (optional): A custom exception class to be raised on timeout.
Usage
Basic Usage
from timeout_decorator import timeout_decorator
@timeout_decorator(5) # 5 seconds timeout
def my_function():
# Your code here
pass
@timeout_decorator(10) # 10 seconds timeout
async def my_async_function():
# Your async code here
pass
Advanced Usage
from timeout_decorator import timeout_decorator
def on_timeout(func_name, *args, **kwargs):
print(f"Function {func_name} timed out!")
class MyTimeoutError(Exception):
pass
@timeout_decorator(5, callback=on_timeout, custom_error=MyTimeoutError)
def my_function():
# Your code here
pass
# Usage with synchronous function
try:
result = my_function()
except MyTimeoutError:
print("Caught custom timeout error")
# Usage with asynchronous function
@timeout_decorator(5, callback=on_timeout, custom_error=MyTimeoutError)
async def my_async_function():
# Your async code here
pass
import asyncio
async def main():
try:
result = await my_async_function()
except MyTimeoutError:
print("Caught custom timeout error")
asyncio.run(main())
Tests
run
pytest tests/test_timeout_decorator.py -v
Notes
For synchronous functions, the decorator uses a ThreadPoolExecutor to implement the timeout.
For asynchronous functions, it uses asyncio.wait_for().
The decorator automatically detects whether the decorated function is synchronous or asynchronous.
Licence
This project is licensed under the MIT License
Metadata
Release files for python-timeout-request-himanshu007-creator 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_timeout_request_himanshu007_creator-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Release files / python_timeout_request_himanshu007_creator-0.0.1-py3-none-any.whl
| Download URL | python_timeout_request_himanshu007_creator-0.0.1-py3-none-any.whl |
|---|---|
| Size | 3.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ce60a66d5152e9caecbf9095600f21110be864f479b811142ae1094c93d9724e
|
|
BLAKE2b-256 checksum How to use checksums |
1f47858bfc1c600c982dd8b0f0c3754f672d3ee853e9ea4292c713b7892f24ff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.5
|
Security for python-timeout-request-himanshu007-creator 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.