Skip to content

Instantly share code, notes, and snippets.

@dagoof
Created November 17, 2010 13:28
Show Gist options
  • Save dagoof/703381 to your computer and use it in GitHub Desktop.
Save dagoof/703381 to your computer and use it in GitHub Desktop.
throttling decorator
import datetime
from functools import wraps
def total_seconds(td):
return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10.0**6
def throttle(time):
def _throttle(f):
f.time = time
f.time_since_last = datetime.datetime.now() - datetime.timedelta(seconds = time)
def wrapped(*args, **kwargs):
if time < total_seconds(datetime.datetime.now() - f.time_since_last):
f.time_since_last = datetime.datetime.now()
return f(*args, **kwargs)
else:
return None
return wrapped
return _throttle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment