Provide list of reloaded files (#2307)

* Allow access to reloaded files

* Return to simple boolean values

* Resolve before adding to changed files
This commit is contained in:
Adam Hopkins 2021-11-16 10:16:32 +02:00 committed by GitHub
parent 9a9f72ad64
commit abeb8d0bc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -47,16 +47,18 @@ def _get_args_for_reloading():
return [sys.executable] + sys.argv return [sys.executable] + sys.argv
def restart_with_reloader(): def restart_with_reloader(changed=None):
"""Create a new process and a subprocess in it with the same arguments as """Create a new process and a subprocess in it with the same arguments as
this one. this one.
""" """
reloaded = ",".join(changed) if changed else ""
return subprocess.Popen( return subprocess.Popen(
_get_args_for_reloading(), _get_args_for_reloading(),
env={ env={
**os.environ, **os.environ,
"SANIC_SERVER_RUNNING": "true", "SANIC_SERVER_RUNNING": "true",
"SANIC_RELOADER_PROCESS": "true", "SANIC_RELOADER_PROCESS": "true",
"SANIC_RELOADED_FILES": reloaded,
}, },
) )
@ -94,24 +96,27 @@ def watchdog(sleep_interval, app):
try: try:
while True: while True:
need_reload = False
changed = set()
for filename in itertools.chain( for filename in itertools.chain(
_iter_module_files(), _iter_module_files(),
*(d.glob("**/*") for d in app.reload_dirs), *(d.glob("**/*") for d in app.reload_dirs),
): ):
try: try:
check = _check_file(filename, mtimes) if _check_file(filename, mtimes):
path = (
filename
if isinstance(filename, str)
else filename.resolve()
)
changed.add(str(path))
except OSError: except OSError:
continue continue
if check: if changed:
need_reload = True
if need_reload:
worker_process.terminate() worker_process.terminate()
worker_process.wait() worker_process.wait()
worker_process = restart_with_reloader() worker_process = restart_with_reloader(changed)
sleep(sleep_interval) sleep(sleep_interval)
except KeyboardInterrupt: except KeyboardInterrupt: