philiprehberger-func-timeout
Add a timeout to any function call, sync or async.
Installation
pip install philiprehberger-func-timeout
Usage
from philiprehberger_func_timeout import timeout
import time
@timeout(2.0)
def slow_computation() -> str:
time.sleep(10)
return "done"
slow_computation() # raises TimeoutError after 2 seconds
Async functions
@timeout(1.5)
async def fetch_data(url: str) -> str:
await asyncio.sleep(10)
return "data"
await fetch_data("https://example.com") # raises TimeoutError after 1.5s
Fallback value
@timeout(2.0, fallback="default")
def risky_call() -> str:
time.sleep(10)
return "result"
risky_call() # returns "default" instead of raising
Custom exception
class MyError(Exception):
def __init__(self, seconds: float) -> None:
super().__init__(f"Took too long: {seconds}s")
@timeout(3.0, exception=MyError)
def slow() -> None:
time.sleep(10)
One-shot execution
from philiprehberger_func_timeout import run_with_timeout, async_run_with_timeout
import asyncio, time
# Sync: call any function with a timeout, no decorator required
result = run_with_timeout(time.sleep, 0.01, timeout=1)
# Async: wrap any awaitable
async def main() -> None:
await async_run_with_timeout(asyncio.sleep(0.01), timeout=1)
asyncio.run(main())
Retry
from philiprehberger_func_timeout import retry
@retry(attempts=3, delay=1.0, backoff=2.0)
def fetch_data(url: str) -> str:
return requests.get(url).text
result = fetch_data("https://api.example.com/data")
API
| Function / Class | Description |
|---|---|
timeout(seconds, *, fallback, exception) |
Decorator that adds a timeout to sync or async functions |
TimeoutError |
Raised on timeout; has a .seconds attribute |
timeout_context(seconds, *, fallback, exception) |
Context manager that raises on timeout |
retry(attempts, delay, *, backoff, on_error) |
Decorator that retries a function on failure with optional backoff |
run_with_timeout(fn, *args, timeout, **kwargs) |
One-shot sync call with a timeout; forwards args/kwargs to fn |
async_run_with_timeout(coro, timeout) |
Await an awaitable with a timeout; raises this package's TimeoutError |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this project useful:
License
Release files for philiprehberger-func-timeout 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| philiprehberger_func_timeout-0.3.0.tar.gz | 176.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| philiprehberger_func_timeout-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 182.6 kB
Release files / philiprehberger_func_timeout-0.3.0.tar.gz
| Download URL | philiprehberger_func_timeout-0.3.0.tar.gz |
|---|---|
| Size | 176.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0d12e07edcdc481ffdb16bec915d879437d3b64ef079c695e7dce11a0f284a81
|
|
BLAKE2b-256 checksum How to use checksums |
ec801437ead7147acd22e835c97d2fafcf89d0abdf5341f9d39e232e6b7ca072
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Release files / philiprehberger_func_timeout-0.3.0-py3-none-any.whl
| Download URL | philiprehberger_func_timeout-0.3.0-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cda25cfe1ce6afe5358b45e44247c85ad01b62faade841a20a871f04e5b2f888
|
|
BLAKE2b-256 checksum How to use checksums |
adab20b2c548593263a9bbfdf73532cdb5c93fe2a23cf56edcb081e9e7f34284
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|