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

Sync functions

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)

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

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.1.7.tar.gz (5.1 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.1.7-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_func_timeout-0.1.7.tar.gz
Algorithm Hash digest
SHA256 7009dda28ceca3b77dd7b7fe034ec9749eaeb5f601551dbd2d79c792d1b81724
MD5 68e43e16c9b78b980854e32b0153e3d0
BLAKE2b-256 7df16a27d522a87058c1523afe9d24d5a8cded6114e7ded3d32917e569f2b5f0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_func_timeout-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 62ad3c40e06ae8af63237cfdd54275d2da146aa8a4fdea45e04007b1e6d46064
MD5 0ee328c5178ee875add955e631ec0863
BLAKE2b-256 2be268bb132cf865b50672f9b2f4aa6fdf48a696a73b42aae3868c6a8f253a5c

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