Skip to content

Commit

Permalink
Fix the auto_reloader to work when the executable was launched with a…
Browse files Browse the repository at this point in the history
… module, rather than a script. (#1501)
  • Loading branch information
ashleysommer authored and sjsadowski committed Mar 3, 2019
1 parent 34fe26e commit 4260528
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions sanic/reloader_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,31 @@ def _iter_module_files():
def _get_args_for_reloading():
"""Returns the executable."""
rv = [sys.executable]
rv.extend(sys.argv)
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)
return rv


def restart_with_reloader():
"""Create a new process and a subprocess in it with the same arguments as
this one.
"""
cwd = os.getcwd()
args = _get_args_for_reloading()
new_environ = os.environ.copy()
new_environ["SANIC_SERVER_RUNNING"] = "true"
cmd = " ".join(args)
worker_process = Process(
target=subprocess.call,
args=(cmd,),
kwargs=dict(shell=True, env=new_environ),
kwargs={"cwd": cwd, "shell": True, "env": new_environ},
)
worker_process.start()
return worker_process
Expand Down

0 comments on commit 4260528

Please sign in to comment.