From 85edd94805cf1cebcde04f7871da712fa95acc97 Mon Sep 17 00:00:00 2001 From: Ivan Gotovchits Date: Thu, 10 Aug 2023 13:13:07 +0000 Subject: [PATCH] simplfies and fixes the debouncer main loop Makes it more readable and fixes a few issues, see #999 and #1000 --- src/watchdog/utils/event_debouncer.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/watchdog/utils/event_debouncer.py b/src/watchdog/utils/event_debouncer.py index b8f2a016..cb483157 100644 --- a/src/watchdog/utils/event_debouncer.py +++ b/src/watchdog/utils/event_debouncer.py @@ -37,24 +37,27 @@ def stop(self): super().stop() self._cond.notify() + def time_to_flush(self, started): + return time.monotonic() - started > self.debounce_interval_seconds + def run(self): with self._cond: - while True: + while self.should_keep_running(): started = time.monotonic() - # Wait for first event (or shutdown). - self._cond.wait() if self.debounce_interval_seconds: - # Wait for additional events (or shutdown) until the debounce interval passes. while self.should_keep_running(): - if self._cond.wait(timeout=self.debounce_interval_seconds): - if (time.monotonic() - started > self.debounce_interval_seconds): - break - else: + timed_out = not self._cond.wait( + timeout=self.debounce_interval_seconds + ) + if timed_out or self.time_to_flush(started): break - - if not self.should_keep_running(): - break + else: + self._cond.wait() events = self._events self._events = [] self.events_callback(events) + + # send any events before leaving + if self._events: + self.events_callback(events)