WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit bdd668d

Browse files
rayludavidism
authored andcommitted
reload as soon as trigger_reload is called
1 parent be7fa8c commit bdd668d

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Unreleased
88
- The debugger pin fails after 10 attempts instead of 11. :pr:`3020`
99
- The multipart form parser handles a ``\r\n`` sequence at a chunk boundary.
1010
:issue:`3065`
11+
- Improve CPU usage during Watchdog reloader. :issue:`3054`
1112

1213

1314
Version 3.1.3

src/werkzeug/_reloader.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626

2727
_stat_ignore_scan = tuple(prefix)
2828
del prefix
29+
# Ignore __pycache__ since a change there will always have a change to
30+
# the source file (or initial pyc file) as well. Ignore common version control
31+
# internals. Ignore common tool caches.
2932
_ignore_common_dirs = {
3033
"__pycache__",
3134
".git",
@@ -86,9 +89,6 @@ def _find_stat_paths(
8689
parent_has_py = {os.path.dirname(path): True}
8790

8891
for root, dirs, files in os.walk(path):
89-
# Optimizations: ignore system prefixes, __pycache__ will
90-
# have a py or pyc module at the import path, ignore some
91-
# common known dirs such as version control and tool caches.
9292
if (
9393
root.startswith(_stat_ignore_scan)
9494
or os.path.basename(root) in _ignore_common_dirs
@@ -325,7 +325,7 @@ def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
325325
trigger_reload = self.trigger_reload
326326

327327
class EventHandler(PatternMatchingEventHandler):
328-
def on_any_event(self, event: FileModifiedEvent): # type: ignore
328+
def on_any_event(self, event: FileModifiedEvent) -> None: # type: ignore[override]
329329
if event.event_type not in {
330330
EVENT_TYPE_CLOSED,
331331
EVENT_TYPE_CREATED,
@@ -345,26 +345,21 @@ def on_any_event(self, event: FileModifiedEvent): # type: ignore
345345

346346
self.name = f"watchdog ({reloader_name})"
347347
self.observer = Observer()
348-
# Extra patterns can be non-Python files, match them in addition
349-
# to all Python files in default and extra directories. Ignore
350-
# __pycache__ since a change there will always have a change to
351-
# the source file (or initial pyc file) as well. Ignore Git and
352-
# Mercurial internal changes.
353-
extra_patterns = [p for p in self.extra_files if not os.path.isdir(p)]
348+
extra_patterns = (p for p in self.extra_files if not os.path.isdir(p))
354349
self.event_handler = EventHandler(
355350
patterns=["*.py", "*.pyc", "*.zip", *extra_patterns],
356351
ignore_patterns=[
357352
*[f"*/{d}/*" for d in _ignore_common_dirs],
358353
*self.exclude_patterns,
359354
],
360355
)
361-
self.should_reload = False
356+
self.should_reload = threading.Event()
362357

363358
def trigger_reload(self, filename: str | bytes) -> None:
364359
# This is called inside an event handler, which means throwing
365360
# SystemExit has no effect.
366361
# https://github.com/gorakhargosh/watchdog/issues/294
367-
self.should_reload = True
362+
self.should_reload.set()
368363
self.log_reload(filename)
369364

370365
def __enter__(self) -> ReloaderLoop:
@@ -377,9 +372,8 @@ def __exit__(self, exc_type, exc_val, exc_tb): # type: ignore
377372
self.observer.join()
378373

379374
def run(self) -> None:
380-
while not self.should_reload:
375+
while not self.should_reload.wait(timeout=self.interval):
381376
self.run_step()
382-
time.sleep(self.interval)
383377

384378
sys.exit(3)
385379

@@ -393,7 +387,7 @@ def run_step(self) -> None:
393387
self.event_handler, path, recursive=True
394388
)
395389
except OSError:
396-
# Clear this path from list of watches We don't want
390+
# Clear this path from list of watches. We don't want
397391
# the same error message showing again in the next
398392
# iteration.
399393
self.watches[path] = None

0 commit comments

Comments
 (0)