Files
tianxuan/_restart_server.py

33 lines
910 B
Python

import subprocess, time, urllib.request, os, sys
PROJ_DIR = r"C:\Users\25044\Desktop\Proj\天璇"
PYTHON_EXE = os.path.join(PROJ_DIR, "runtime", "python", "python.exe")
os.chdir(PROJ_DIR)
server = subprocess.Popen(
[PYTHON_EXE, "manage.py", "runserver", "127.0.0.1:8765", "--noreload"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
print(f"SERVER_PID={server.pid}")
# Poll with backoff: fast early, then slower
deadline = time.time() + 60
attempt = 0
while time.time() < deadline:
attempt += 1
try:
resp = urllib.request.urlopen("http://127.0.0.1:8765/", timeout=3)
if resp.status == 200:
print("SERVER_READY")
sys.exit(0)
except Exception as e:
if attempt % 10 == 0:
print(f"WAITING... attempt={attempt} err={type(e).__name__}")
time.sleep(1)
print("SERVER_START_TIMEOUT")
server.kill()
sys.exit(1)