Skip to content

Commit

Permalink
Provide list of reloaded files (sanic-org#2307)
Browse files Browse the repository at this point in the history
* Allow access to reloaded files

* Return to simple boolean values

* Resolve before adding to changed files
  • Loading branch information
ahopkins authored and ChihweiLHBird committed Jun 1, 2022
1 parent a4428fb commit f6806c2
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions sanic/reloader_helpers.py
Expand Up @@ -47,16 +47,18 @@ def _get_args_for_reloading():
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
this one.
"""
reloaded = ",".join(changed) if changed else ""
return subprocess.Popen(
_get_args_for_reloading(),
env={
**os.environ,
"SANIC_SERVER_RUNNING": "true",
"SANIC_RELOADER_PROCESS": "true",
"SANIC_RELOADED_FILES": reloaded,
},
)

Expand Down Expand Up @@ -94,24 +96,27 @@ def interrupt_self(*args):

try:
while True:
need_reload = False

changed = set()
for filename in itertools.chain(
_iter_module_files(),
*(d.glob("**/*") for d in app.reload_dirs),
):
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:
continue

if check:
need_reload = True

if need_reload:
if changed:
worker_process.terminate()
worker_process.wait()
worker_process = restart_with_reloader()
worker_process = restart_with_reloader(changed)

sleep(sleep_interval)
except KeyboardInterrupt:
Expand Down

0 comments on commit f6806c2

Please sign in to comment.