From 1097413273be7f412c1201bab67d430dd2fb9682 Mon Sep 17 00:00:00 2001 From: Christian Sattler Date: Sat, 11 Dec 2021 01:27:32 +0100 Subject: [PATCH 1/4] Eliminate timeout in waiting on event queue. --- src/watchdog/observers/api.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/watchdog/observers/api.py b/src/watchdog/observers/api.py index 6a30903d4..647450b6b 100644 --- a/src/watchdog/observers/api.py +++ b/src/watchdog/observers/api.py @@ -159,6 +159,9 @@ class EventDispatcher(BaseThread): ``float`` """ + _stop_event = object() + """Event inserted into the queue to signal a requested stop.""" + def __init__(self, timeout=DEFAULT_OBSERVER_TIMEOUT): BaseThread.__init__(self) self._event_queue = EventQueue() @@ -169,6 +172,13 @@ def timeout(self): """Event queue block timeout.""" return self._timeout + def stop(self): + BaseThread.stop(self) + try: + self.event_queue.put_nowait(EventDispatcher._stop_event) + except queue.Full: + pass + @property def event_queue(self): """The event queue which is populated with file system events @@ -176,7 +186,7 @@ def event_queue(self): thread.""" return self._event_queue - def dispatch_events(self, event_queue, timeout): + def dispatch_events(self, event_queue): """Override this method to consume events from an event queue, blocking on the queue for the specified timeout before raising :class:`queue.Empty`. @@ -184,11 +194,6 @@ def dispatch_events(self, event_queue, timeout): Event queue to populate with one set of events. :type event_queue: :class:`EventQueue` - :param timeout: - Interval period (in seconds) to wait before timing out on the - event queue. - :type timeout: - ``float`` :raises: :class:`queue.Empty` """ @@ -196,7 +201,7 @@ def dispatch_events(self, event_queue, timeout): def run(self): while self.should_keep_running(): try: - self.dispatch_events(self.event_queue, self.timeout) + self.dispatch_events(self.event_queue) except queue.Empty: continue @@ -360,8 +365,11 @@ def unschedule_all(self): def on_thread_stop(self): self.unschedule_all() - def dispatch_events(self, event_queue, timeout): - event, watch = event_queue.get(block=True, timeout=timeout) + def dispatch_events(self, event_queue): + entry = event_queue.get(block=True) + if entry is EventDispatcher._stop_event: + return + event, watch = entry with self._lock: # To allow unschedule/stop and safe removal of event handlers From 396c333dd182f361733ca9e82d4a41678cd0e351 Mon Sep 17 00:00:00 2001 From: Christian Sattler Date: Sat, 11 Dec 2021 01:52:14 +0100 Subject: [PATCH 2/4] Fix broken documentation of timeout in EventDispatcher.. --- src/watchdog/observers/api.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/watchdog/observers/api.py b/src/watchdog/observers/api.py index 647450b6b..e53158737 100644 --- a/src/watchdog/observers/api.py +++ b/src/watchdog/observers/api.py @@ -154,7 +154,8 @@ class EventDispatcher(BaseThread): that dispatch events from an event queue to appropriate event handlers. :param timeout: - Event queue blocking timeout (in seconds). + Timeout value (in seconds) passed to emitters + constructions in the child class BaseObserver. :type timeout: ``float`` """ @@ -169,7 +170,7 @@ def __init__(self, timeout=DEFAULT_OBSERVER_TIMEOUT): @property def timeout(self): - """Event queue block timeout.""" + """Timeout value to construct emitters with.""" return self._timeout def stop(self): From e9771e6a59a8b0cfd399c4f709427e8aae7035fd Mon Sep 17 00:00:00 2001 From: Christian Sattler Date: Sat, 11 Dec 2021 12:43:48 +0100 Subject: [PATCH 3/4] Update the changelog. --- changelog.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/changelog.rst b/changelog.rst index 8ad95138b..8868407e1 100644 --- a/changelog.rst +++ b/changelog.rst @@ -8,8 +8,8 @@ Changelog 2021-xx-xx • `full history `__ -- -- Thanks to our beloved contributors: @ +- Eliminate timeout in waiting on event queue. (`#861 `_) +- Thanks to our beloved contributors: @sattlerc 2.1.6 ~~~~~ @@ -60,7 +60,7 @@ Changelog - [mac] Fix relative path handling for non-recursive watch. (`#797 `_) - [windows] On PyPy, events happening right after ``start()`` were missed. Add a workaround for that. (`#796 `_) -- Thanks to our beloved contributors: @oprypin, @CCP-Aporia, @BoboTiG +w- Thanks to our beloved contributors: @oprypin, @CCP-Aporia, @BoboTiG 2.1.1 ~~~~~ From 6a015ec9b4c3ac8325b38134c5fe261c0085ef67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Sat, 11 Dec 2021 12:56:19 +0100 Subject: [PATCH 4/4] Update changelog.rst --- changelog.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.rst b/changelog.rst index 8868407e1..a82121253 100644 --- a/changelog.rst +++ b/changelog.rst @@ -60,7 +60,7 @@ Changelog - [mac] Fix relative path handling for non-recursive watch. (`#797 `_) - [windows] On PyPy, events happening right after ``start()`` were missed. Add a workaround for that. (`#796 `_) -w- Thanks to our beloved contributors: @oprypin, @CCP-Aporia, @BoboTiG +- Thanks to our beloved contributors: @oprypin, @CCP-Aporia, @BoboTiG 2.1.1 ~~~~~