Created
May 1, 2016 04:42
-
-
Save hardik-vala/4f95ee17aae5ce4c323cd1d4cb18e013 to your computer and use it in GitHub Desktop.
Example illustrating how to use a decorator to notify you via text message if a long running function crashes.
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
""" | |
Example illustrating how to use a decorator to notify you via text message if a | |
long running function crashes. | |
""" | |
import os | |
import time | |
# Phone number (You may want to change this). | |
NUMBER = 1234567890 | |
def wrapper(f): | |
def notifier(*args, **kwargs): | |
try: | |
f(args, kwargs) | |
except Exception as e: | |
# Check out textbelt.com to learn more about how to use their API. | |
os.system("curl -X POST http://textbelt.com/canada -d number=%s -d \"message=%s\"" % (NUMBER, str(e))) | |
return notifier | |
@wrapper | |
def my_long_running_functio(arg, kwarg="kwarg"): | |
time.sleep(5) | |
raise RuntimeError("MY ERROR") | |
if __name__ == '__main__': | |
my_long_running_functio("arg") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment