Fix the auto_reloader to work when the executable was launched with a module, rather than a script. (#1501)

This commit is contained in:
Ashley Sommer 2019-03-04 03:03:26 +10:00 committed by Stephen Sadowski
parent 34fe26e51b
commit 4260528645

View File

@ -36,6 +36,14 @@ def _iter_module_files():
def _get_args_for_reloading(): def _get_args_for_reloading():
"""Returns the executable.""" """Returns the executable."""
rv = [sys.executable] rv = [sys.executable]
main_module = sys.modules["__main__"]
mod_spec = getattr(main_module, "__spec__", None)
if mod_spec:
# Parent exe was launched as a module rather than a script
rv.extend(["-m", mod_spec.name])
if len(sys.argv) > 1:
rv.extend(sys.argv[1:])
else:
rv.extend(sys.argv) rv.extend(sys.argv)
return rv return rv
@ -44,6 +52,7 @@ def restart_with_reloader():
"""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.
""" """
cwd = os.getcwd()
args = _get_args_for_reloading() args = _get_args_for_reloading()
new_environ = os.environ.copy() new_environ = os.environ.copy()
new_environ["SANIC_SERVER_RUNNING"] = "true" new_environ["SANIC_SERVER_RUNNING"] = "true"
@ -51,7 +60,7 @@ def restart_with_reloader():
worker_process = Process( worker_process = Process(
target=subprocess.call, target=subprocess.call,
args=(cmd,), args=(cmd,),
kwargs=dict(shell=True, env=new_environ), kwargs={"cwd": cwd, "shell": True, "env": new_environ},
) )
worker_process.start() worker_process.start()
return worker_process return worker_process