Created
June 10, 2019 18:13
-
-
Save bhpayne/54fb2c8d864d02750c9168ae734fb21e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
import errno | |
import signal | |
def timeout(seconds=10, error_message=os.strerror(errno.ETIME)): | |
""" | |
https://stackoverflow.com/questions/2281850/timeout-function-if-it-takes-too-long-to-finish | |
see also | |
""" | |
def decorator(func): | |
def _handle_timeout(signum, frame): | |
send_email(['ben.is.located@gmail.com'],error_message,'TIMEOUT OCCURRED') | |
raise Exception(error_message) | |
def wrapper(*args, **kwargs): | |
signal.signal(signal.SIGALRM, _handle_timeout) | |
signal.alarm(seconds) | |
try: | |
result = func(*args, **kwargs) | |
finally: | |
signal.alarm(0) | |
return result | |
return wraps(func)(wrapper) | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment