https://github.com/kkristof200/py_timeout
https://github.com/kkristof200/py_timeout
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/kkristof200/py_timeout
- Owner: kkristof200
- License: mit
- Created: 2020-05-22T10:13:44.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-05-23T09:43:50.000Z (about 5 years ago)
- Last Synced: 2025-03-30T16:02:20.957Z (4 months ago)
- Language: Python
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ktimeout
 [](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)
~~~~