時間がかかる処理に対して、タイムアウトを設定したい場合に使用するためのデコレータとして、timeout_decorator.timeout
があるので、使ってみた。
用途として、サーバに対する検索のリクエストを実行した際に、検索に時間がかかる場合にタイムアウトを設定する。
timeout_decorator.timeout
デコレータのパラメータに関数がコールされてからタイムアウトする
秒数を指定。
下記の例の場合、10秒スリープする処理を入れているが、デコレータに5秒でタイムアウトするように設定している。
5秒が経過すると、timeout_decorator.timeout_decorator.TimeoutError
が発生する。
import time import timeout_decorator # 5秒後にタイムアウトする @timeout_decorator.timeout(5) def call_func(): print("Start call_func") time.sleep(10) print("End call_func") if __name__ == '__main__': i = 0 while i < 3: call_func() i += 1
独自のエラーをライズしたい
timeout_decorator.timeout_decorator.TimeoutError
ではなく、独自で定義した
Exceptionクラスをレイズしたい場合、timeout_exception
パラメータにレイズしたい
Exceptionクラスを指定することで可能。
下記の例だと、timeout_exception
パラメータにNewError
クラスを指定。
タイムアウトが発生すると、NewError
クラスがレイズされる。
import time import timeout_decorator class NewError(Exception): pass # 5秒後にタイムアウトする @timeout_decorator.timeout(5, timeout_exception=NewError) def call_func(): print("Start call_func") time.sleep(10) print("End call_func") if __name__ == '__main__': i = 0 while i < 3: call_func() i += 1
時間がかかる検索にはタイムアウトを設定しよう
HTTPリクエストで時間がかかる処理をタイムアウトするために、timeout_decorator.timeout
デコレータを
使ってタイムアウトを設定しよう。
ただ、requestsモジュールにはタイムアウトの機能が存在するので、requestsモジュールを使用する場合は、
requestsモジュールのタイムアウト機能を素直に使いましょう。
import timeout_decorator import requests @timeout_decorator.timeout(10) def call_func(): print("Send Request!!") response = requests.get("http://localhost:8000"params=None) print("Receive Response!!") if __name__ == '__main__': i = 0 while i < 3: call_func() i += 1
最後に
モジュールをインストールして、デコレータの設定のみでタイムアウト機能が実装できた。 他にもタイムアウトを実装したモジュールがあるようなので、用途によって使い分けたほうがいいかと思う。