I wrote it for the first time using a decorator and a signal. A decorator that notifies you if the function does not finish within the specified time. It seems that you can use it to notify you of batches that are taking too long when you run nightly batches on AWS.
pytimekeeper.py
# -*- coding: utf-8 -*-
import signal
import boto.sns
def timeout(limit, topic, subject='Execution Timeout.', body='Please check program.', region='us-east-1'):
'''
How to use:A decorator that notifies you with aws sns if it does not finish within the specified execution time.
@timeout(limit=3600, topic='arn:aws:sns:xxxxxxxx:yyyyyyy')
def long_time_function():
very_very_long_calc()
'''
def notify_aws_sns(signum, frame):
conn = boto.sns.connect_to_region(region)
conn.publish(topic, body, subject)
def __decorator(function):
def __wrapper(*args, **kwargs):
signal.signal(signal.SIGALRM, notify_aws_sns)
signal.alarm(limit)
result = function(*args, **kwargs)
signal.alarm(0)
return result
return __wrapper
return __decorator
Recommended Posts