add timeout to any function
Project description
ktimeout
Description
add timeout to any function
Install
pip install ktimeout
# or
pip3 install ktimeout
Usage
# CHECK 'demo.py' FOR ALL EXAMPLES
from typing import Optional
import time
from ktimeout import timeout
def func_with_arguments(sleep_time: float, extra_print: Optional[str] = None):
while True:
time.sleep(sleep_time)
print('Sleeping', sleep_time, 'sec', extra_print or '')
def func():
func_with_arguments(0.5, extra_print='called from func()')
try:
timeout.run(func, 2)
except Exception as e:
print(e)
try:
timeout.run(
timeout.partial(func_with_arguments, 0.25, extra_print='extra'),
2
)
except Exception as e:
print(e)
try:
with timeout.timeout(1):
while True:
sleep_time = 0.25
time.sleep(sleep_time)
print('Sleeping', sleep_time, 'sec')
except Exception as e:
print(e)
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
ktimeout-0.0.7.tar.gz
(2.1 kB
view details)
Built Distribution
File details
Details for the file ktimeout-0.0.7.tar.gz
.
File metadata
- Download URL: ktimeout-0.0.7.tar.gz
- Upload date:
- Size: 2.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.24.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8a0d75b60837285153c00ad291e60d3f31f582d59d1c3f96f26e257a09faaed |
|
MD5 | 66fddc50d64247195ab7975971bb8f71 |
|
BLAKE2b-256 | ded7aa5dce1f9e0f57a8d7460549d553cdda5af67ccb292f004e9fd72aa67bbc |
File details
Details for the file ktimeout-0.0.7-py3-none-any.whl
.
File metadata
- Download URL: ktimeout-0.0.7-py3-none-any.whl
- Upload date:
- Size: 3.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.24.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6fef369f61b1f7e29053d4e01d159ebd34a18022b956816d6e7ba21a86894a7 |
|
MD5 | c7512828fb05b0bea2675ebbd86f1de6 |
|
BLAKE2b-256 | 298e66a2ac8b51ea4f53522c9604921f5569d2a8ae424b43ad955d22a023875b |