Better handling of subsequent throttle calls

This commit is contained in:
Paulus Schoutsen 2015-01-05 20:50:34 -08:00
parent 1b0143341c
commit b90826c267

View File

@ -289,19 +289,24 @@ class Throttle(object):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
""" """
Wrapper that allows wrapped to be called only once per min_time. Wrapper that allows wrapped to be called only once per min_time.
If we cannot acquire the lock, it is running so return None.
""" """
with lock: if lock.acquire(False):
last_call = wrapper.last_call try:
# Check if method is never called or no_throttle is given last_call = wrapper.last_call
force = last_call is None or kwargs.pop('no_throttle', False)
if force or datetime.now() - last_call > self.min_time: # Check if method is never called or no_throttle is given
force = not last_call or kwargs.pop('no_throttle', False)
result = method(*args, **kwargs) if force or datetime.now() - last_call > self.min_time:
wrapper.last_call = datetime.now()
return result result = method(*args, **kwargs)
else: wrapper.last_call = datetime.now()
return None return result
else:
return None
finally:
lock.release()
wrapper.last_call = None wrapper.last_call = None