-
-
Notifications
You must be signed in to change notification settings - Fork 704
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[watchmedo] Add optional event debouncing for
auto-restart
(#940)
* Add a test for auto-restart on file changes * Implement auto-restart event debouncing * lint * Also protect _stop_process from running in parallel Also refactor kill_process() and fix a bug in it. * Add changlog entry * Move input value checking to beginning of method * Add restart counter and improve test * Add test doc-strings * Update tests/test_0_watchmedo.py Co-authored-by: Mickaël Schoentgen <contact@tiger-222.fr> * Instantly handle shutdown during debouncing * Lint --------- Co-authored-by: Mickaël Schoentgen <contact@tiger-222.fr>
- Loading branch information
Showing
5 changed files
with
214 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import logging | ||
import threading | ||
|
||
from watchdog.utils import BaseThread | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class EventDebouncer(BaseThread): | ||
"""Background thread for debouncing event handling. | ||
When an event is received, wait until the configured debounce interval | ||
passes before calling the callback. If additional events are received | ||
before the interval passes, reset the timer and keep waiting. When the | ||
debouncing interval passes, the callback will be called with a list of | ||
events in the order in which they were received. | ||
""" | ||
def __init__(self, debounce_interval_seconds, events_callback): | ||
super().__init__() | ||
self.debounce_interval_seconds = debounce_interval_seconds | ||
self.events_callback = events_callback | ||
|
||
self._events = [] | ||
self._cond = threading.Condition() | ||
|
||
def handle_event(self, event): | ||
with self._cond: | ||
self._events.append(event) | ||
self._cond.notify() | ||
|
||
def stop(self): | ||
with self._cond: | ||
super().stop() | ||
self._cond.notify() | ||
|
||
def run(self): | ||
with self._cond: | ||
while True: | ||
# 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 not self._cond.wait(timeout=self.debounce_interval_seconds): | ||
break | ||
|
||
if not self.should_keep_running(): | ||
break | ||
|
||
events = self._events | ||
self._events = [] | ||
self.events_callback(events) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters