A library that can be used when you want to use a timeout in a library that does not implement timeout in Python
timeout-decorator https://github.com/pnpnpn/timeout-decorator
sample
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 :)"
Execution result
$ python test_timeoput.py
0
1
2
3
4
test timed out :(
Recommended Posts