/
ilya.petrash
/
SimplePyScripts
Обзор
Документация
Войти
/
ilya.petrash
/
SimplePyScripts
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
timeout_block/main.py
52 строки
987 B
gil9red
Refactoring. Using black code style
28 июн 2023, 13:07
28 июн 2023, 13:07
28dcee9
Код
Авторство
О чём код?
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = "ipetrash" import time from kthread import KThread def timeout(seconds=None, raise_timeout=False): def wrapper(function_to_decorate): def the_wrapper_around_the_original_function(*args, **kwargs): thread = KThread(target=lambda: function_to_decorate(*args, **kwargs)) thread.start() thread.join(timeout=seconds) if thread.is_alive(): thread.kill() if raise_timeout: raise TimeoutError() return the_wrapper_around_the_original_function return wrapper @timeout(seconds=3) def unlimited_wait(): i = 0 while True: i += 1 print(i) time.sleep(1) @timeout(seconds=3, raise_timeout=True) def unlimited_wait2(): i = 0 while True: i += 1 print(i) time.sleep(1) if __name__ == "__main__": unlimited_wait() unlimited_wait2()