Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/watchdog/utils/event_debouncer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import logging
import time
import threading

from watchdog.utils import BaseThread
Expand Down Expand Up @@ -36,21 +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:
# Wait for first event (or shutdown).
self._cond.wait()

while self.should_keep_running():
started = time.monotonic()
if self.debounce_interval_seconds:
# Wait for additional events (or shutdown) until the debounce interval passes.
while self.should_keep_running():
if not self._cond.wait(timeout=self.debounce_interval_seconds):
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)