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 sys
from multiprocessing import Process
from time import sleep
@ -35,35 +34,27 @@ def _iter_module_files():
def _get_args_for_reloading():
"""Returns the executable."""
rv = [sys.executable]
main_module = sys.modules["__main__"]
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:
# 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
return [sys.executable, "-m", mod_spec.name] + sys.argv[1:]
return [sys.executable] + sys.argv
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={"cwd": cwd, "shell": True, "env": new_environ},
return subprocess.Popen(
_get_args_for_reloading(),
cwd=os.getcwd(),
env={**os.environ, "SANIC_SERVER_RUNNING": "true"},
)
worker_process.start()
return worker_process
def kill_process_children_unix(pid):