Better handling of subsequent throttle calls
This commit is contained in:
parent
1b0143341c
commit
b90826c267
1 changed files with 15 additions and 10 deletions
|
@ -289,11 +289,14 @@ 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):
|
||||||
|
try:
|
||||||
last_call = wrapper.last_call
|
last_call = wrapper.last_call
|
||||||
|
|
||||||
# Check if method is never called or no_throttle is given
|
# Check if method is never called or no_throttle is given
|
||||||
force = last_call is None or kwargs.pop('no_throttle', False)
|
force = not last_call or kwargs.pop('no_throttle', False)
|
||||||
|
|
||||||
if force or datetime.now() - last_call > self.min_time:
|
if force or datetime.now() - last_call > self.min_time:
|
||||||
|
|
||||||
|
@ -302,6 +305,8 @@ class Throttle(object):
|
||||||
return result
|
return result
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
finally:
|
||||||
|
lock.release()
|
||||||
|
|
||||||
wrapper.last_call = None
|
wrapper.last_call = None
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue