Simplify autoreloader, don't need multiprocessing.Process. Now works on OSX py38.

This commit is contained in:
L. Kärkkäinen 2020-03-31 13:03:00 +03:00
parent 59ccf711fa
commit 8ebac9bca0

View File

@ -3,7 +3,6 @@ import signal
import subprocess import subprocess
import sys import sys
from multiprocessing import Process
from time import sleep from time import sleep
@ -35,35 +34,27 @@ def _iter_module_files():
def _get_args_for_reloading(): def _get_args_for_reloading():
"""Returns the executable.""" """Returns the executable."""
rv = [sys.executable]
main_module = sys.modules["__main__"] main_module = sys.modules["__main__"]
mod_spec = getattr(main_module, "__spec__", None) mod_spec = getattr(main_module, "__spec__", None)
if sys.argv[0] in ("", "-c"):
raise RuntimeError(
f"Autoreloader cannot work with argv[0]={sys.argv[0]!r}"
)
if mod_spec: if mod_spec:
# Parent exe was launched as a module rather than a script # Parent exe was launched as a module rather than a script
rv.extend(["-m", mod_spec.name]) return [sys.executable, "-m", mod_spec.name] + sys.argv[1:]
if len(sys.argv) > 1: return [sys.executable] + sys.argv
rv.extend(sys.argv[1:])
else:
rv.extend(sys.argv)
return rv
def restart_with_reloader(): 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() return subprocess.Popen(
args = _get_args_for_reloading() _get_args_for_reloading(),
new_environ = os.environ.copy() cwd=os.getcwd(),
new_environ["SANIC_SERVER_RUNNING"] = "true" env={**os.environ, "SANIC_SERVER_RUNNING": "true"},
cmd = " ".join(args)
worker_process = Process(
target=subprocess.call,
args=(cmd,),
kwargs={"cwd": cwd, "shell": True, "env": new_environ},
) )
worker_process.start()
return worker_process
def kill_process_children_unix(pid): def kill_process_children_unix(pid):