44
43

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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 :(
44
43
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
44
43

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?