From 8ebac9bca0bcef06096ee4c6c19c8a6cf325f418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=2E=20K=C3=A4rkk=C3=A4inen?= Date: Tue, 31 Mar 2020 13:03:00 +0300 Subject: [PATCH] Simplify autoreloader, don't need multiprocessing.Process. Now works on OSX py38. --- sanic/reloader_helpers.py | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/sanic/reloader_helpers.py b/sanic/reloader_helpers.py index 5e1338a4..89242439 100644 --- a/sanic/reloader_helpers.py +++ b/sanic/reloader_helpers.py @@ -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):