LoginSignup
43
42

More than 5 years have passed since last update.

pythonでタイムアウトを簡単に実装したい

Last updated at Posted at 2016-06-07

Pythonでタイムアウトが実装されていないライブラリでタイムアウトを使いたい時とかに使えそうなライブラリ

timeout-decorator
https://github.com/pnpnpn/timeout-decorator

サンプル

test_timeout.py
import time
import timeout_decorator


def very_long_function():
    for i in range(100):
        print i
        time.sleep(1)


@timeout_decorator.timeout(5)
def test():
    very_long_function()


if __name__ == '__main__':
    try:
        test()
    except:
        print "test timed out :("
    else:
        print "test finished successfully :)"

実行結果

$ python test_timeoput.py
0
1
2
3
4
test timed out :(
43
42
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
43
42