#!/usr/bin/env python3 # -*- coding: utf-8 -*- import sys import time from time_decorator import timeout, TimeoutError class TimeoutDecoratorTest(object): def test(self, sleep_sec, timeout_sec): @timeout(timeout_sec) def inner_test(): print("Start sleep " + str(sleep_sec) + " seconds.") print("Timeout in " + str(timeout_sec) + " seconds.") time.sleep(sleep_sec) try: inner_test() print("End sleep.") except TimeoutError: print("Timeout!") if __name__ == '__main__': sleep_sec = int(sys.argv[1]) timeout_sec = int(sys.argv[2]) t = TimeoutDecoratorTest() t.test(sleep_sec, timout_sec)
タイムアウト処理を行うためのライブラリ
pip install wrap-timeout-decorator
import wrapt_timeout_decorator
@wrapt_timeout_decorator.timeout(dec_timeout=30)
def func():
while True:
pass
if __name__ == ‘__main__’:
func()
タイムアウトの設定をするのね