Created
November 17, 2010 13:28
-
-
Save dagoof/703381 to your computer and use it in GitHub Desktop.
throttling decorator
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
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