From b90826c26769184aa093a8a2568ae60135c32eba Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 5 Jan 2015 20:50:34 -0800 Subject: [PATCH] Better handling of subsequent throttle calls --- homeassistant/util.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/homeassistant/util.py b/homeassistant/util.py index 1932d9ada63..99834b1ea2c 100644 --- a/homeassistant/util.py +++ b/homeassistant/util.py @@ -289,19 +289,24 @@ class Throttle(object): def wrapper(*args, **kwargs): """ 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: - last_call = wrapper.last_call - # Check if method is never called or no_throttle is given - force = last_call is None or kwargs.pop('no_throttle', False) + if lock.acquire(False): + try: + last_call = wrapper.last_call - 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) - wrapper.last_call = datetime.now() - return result - else: - return None + if force or datetime.now() - last_call > self.min_time: + + result = method(*args, **kwargs) + wrapper.last_call = datetime.now() + return result + else: + return None + finally: + lock.release() wrapper.last_call = None