From 1ca29bdd9bfe55acf5f31340baa072bad0ab86cf Mon Sep 17 00:00:00 2001 From: Massimo Di Pierro Date: Mon, 9 Sep 2024 00:51:36 +0200 Subject: [PATCH] watch for changes only apps non-excluded, fxies issue #919 --- py4web/core.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/py4web/core.py b/py4web/core.py index 10dae504..9f3cee79 100644 --- a/py4web/core.py +++ b/py4web/core.py @@ -1611,10 +1611,21 @@ def watch_folder_event_loop(apps_folder): async def watch_folder(apps_folder): """Async function that watches a folder for changes""" click.echo(f"watching ({mode}-mode) python file changes in: {apps_folder}") + + # Then load all the apps as submodules + if os.environ.get("PY4WEB_APP_NAMES"): + app_names = os.environ.get("PY4WEB_APP_NAMES").split(",") + else: + app_names = None + async for changes in awatch(os.path.join(apps_folder)): apps = [] for subpath in [pathlib.Path(pair[1]) for pair in changes]: name = subpath.relative_to(apps_folder).parts[0] + # ignore apps not listed in app names + if app_names is not None and name not in app_names: + continue + # record the name of the app that changed if subpath.suffix == ".py": apps.append(name) ## manage `app_watch_handler` decorators @@ -1625,11 +1636,13 @@ async def watch_folder(apps_folder): APP_WATCH["tasks"][handler] = {} APP_WATCH["tasks"][handler][subpath.as_posix()] = True + # reimport the apps the changed for name in apps: if mode == "lazy": DIRTY_APPS[name] = True else: Reloader.import_app(name) + ## in 'lazy' mode it's done in bottle's 'before_request' hook if mode != "lazy": try_app_watch_tasks()