Skip to main content

Add a timeout to any function call, sync or async.

Project description

philiprehberger-func-timeout

Tests PyPI version Last updated

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)

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

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_func_timeout-0.2.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_func_timeout-0.2.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_func_timeout-0.2.0.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_func_timeout-0.2.0.tar.gz
Algorithm Hash digest
SHA256 6fa7be84eb950a0da39f2a92994824879c290f53147334792d9b9e367a6a2e89
MD5 a08180f8dc82e8170825928ee6013df9
BLAKE2b-256 f7ab53c8a5648515c5c1ee01421cc16a26236f466361e68c7231e81bccf5b47e

See more details on using hashes here.

File details

Details for the file philiprehberger_func_timeout-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_func_timeout-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6a879eeff0710f6d77b7b015cfa8472bd00f0546872dae3712e9e392f8201e7d
MD5 5b5720b0a54030e979ecc0760590dfdf
BLAKE2b-256 55ec589fafd816525bc3abe557d2ea799ba3970afa39919a136d55d56de68578

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page