Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime
- from collections import deque
- from functools import wraps
- from django.core.cache import cache
- def timed_task(f):
- @wraps(f)
- def wrapper(*args, **kwargs):
- """Simple decorator to cache previous 10 execution times
- """
- start = datetime.now()
- rslt = f(*args, **kwargs)
- diff = datetime.now() - start
- cache_key = '%s.timings' % args[0].__class__.__name__
- last_ten = cache.get(cache_key, deque([], 10))
- last_ten.append(diff.seconds + float(diff.microseconds)/1000000)
- if not cache.add(cache_key, last_ten, 86400):
- cache.set(cache_key, last_ten, 86400)
- return rslt
- return wrapper
Add Comment
Please, Sign In to add comment