Timeout utility class to wait for any function output and interact with it in given time
Project description
TimeoutSampler
Utility class for waiting to any function output and interact with it in given time.
Installation
python3 -m pip install timeout-sampler
Usage
from random import randint
from timeout_sampler import TimeoutSampler
def random_number(start, end):
if isinstance(start, str) or isinstance(end, str):
raise TypeError("start and end must be int type")
if end <= start:
raise ValueError("End must be greater than start")
return randint(start, end)
samples = TimeoutSampler(
wait_timeout=60,
sleep=1,
func=random_number,
start=1,
end=10,
)
for sample in samples:
if sample == 5:
break
# Raise `TimeoutExpiredError` since we continue on `ValueError` exception
for sample in TimeoutSampler(
wait_timeout=1,
sleep=1,
func=random_number,
exceptions_dict={ValueError: []},
start=10,
end=1,
):
if sample:
return
# Raise `TimeoutExpiredError` since we continue on `ValueError` with match error exception
for sample in TimeoutSampler(
wait_timeout=1,
sleep=1,
func=raise_value_error,
exceptions_dict={ValueError: ["End must be greater than start"]},
start=10,
end=1,
):
if sample:
return
# Raise TimeoutExpiredError immediately since ValueError exception error do not match the error in the exceptions_dict
for sample in TimeoutSampler(
wait_timeout=1,
sleep=1,
func=raise_value_error,
exceptions_dict={ValueError: ["some other error"]},
start=10,
end=1,
):
if sample:
return
# Use as decorator. (Any argument that TimeoutSampler accepts will be passed to the decorated function)
from timeout_sampler import retry
@retry(wait_timeout=60, sleep=1)
def random_number(start, end):
if isinstance(start, str) or isinstance(end, str):
raise TypeError("start and end must be int type")
if end > start:
raise ValueError("End must be greater than start")
return randint(start, end)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
timeout_sampler-1.0.22.tar.gz
(21.5 kB
view details)
File details
Details for the file timeout_sampler-1.0.22.tar.gz.
File metadata
- Download URL: timeout_sampler-1.0.22.tar.gz
- Upload date:
- Size: 21.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f4b7abc32eb56a80642c8584a06ea9cea6802387f572cb28f1deda1684185a7
|
|
| MD5 |
fca4837a454e69479f16456f3f6f39d4
|
|
| BLAKE2b-256 |
3218af47c7b684056e7227fe5e81f024518cbfaab51d98923768506387587ba3
|