Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kkristof200/py_timeout
https://github.com/kkristof200/py_timeout
Last synced: 11 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/kkristof200/py_timeout
- Owner: kkristof200
- License: mit
- Created: 2020-05-22T10:13:44.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-05-23T09:43:50.000Z (over 4 years ago)
- Last Synced: 2024-10-14T09:38:07.643Z (about 1 month ago)
- Language: Python
- Size: 8.79 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ktimeout
![python_version](https://img.shields.io/static/v1?label=Python&message=3.5%20|%203.6%20|%203.7&color=blue) [![PyPI downloads/month](https://img.shields.io/pypi/dm/ktimeout?logo=pypi&logoColor=white)](https://pypi.python.org/pypi/ktimeout)## Description
add timeout to any function## Install
~~~~bash
pip install ktimeout
# or
pip3 install ktimeout
~~~~## Usage
~~~~python
# CHECK 'demo.py' FOR ALL EXAMPLES
from typing import Optional
import timefrom 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)
~~~~