Skip to content

Commit

Permalink
simplfies and fixes the debouncer main loop
Browse files Browse the repository at this point in the history
Makes it more readable and fixes a few issues, see gorakhargosh#999 and gorakhargosh#1000
  • Loading branch information
ivg committed Aug 10, 2023
1 parent 8afb815 commit 85edd94
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/watchdog/utils/event_debouncer.py
Expand Up @@ -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)

0 comments on commit 85edd94

Please sign in to comment.