增强flask健壮度
This commit is contained in:
75
.idea/workspace.xml
generated
75
.idea/workspace.xml
generated
@@ -6,7 +6,8 @@
|
|||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="eceeff5e-51c1-459c-a911-d21ec090a423" name="Changes" comment="20250904-初步功能已完成">
|
<list default="true" id="eceeff5e-51c1-459c-a911-d21ec090a423" name="Changes" comment="20250904-初步功能已完成">
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/Module/DeviceInfo.py" beforeDir="false" afterPath="$PROJECT_DIR$/Module/DeviceInfo.py" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/Module/FlaskSubprocessManager.py" beforeDir="false" afterPath="$PROJECT_DIR$/Module/FlaskSubprocessManager.py" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/Utils/DevDiskImageDeployer.py" beforeDir="false" afterPath="$PROJECT_DIR$/Utils/DevDiskImageDeployer.py" afterDir="false" />
|
||||||
</list>
|
</list>
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
@@ -49,31 +50,32 @@
|
|||||||
<option name="hideEmptyMiddlePackages" value="true" />
|
<option name="hideEmptyMiddlePackages" value="true" />
|
||||||
<option name="showLibraryContents" value="true" />
|
<option name="showLibraryContents" value="true" />
|
||||||
</component>
|
</component>
|
||||||
<component name="PropertiesComponent">{
|
<component name="PropertiesComponent"><![CDATA[{
|
||||||
"keyToString": {
|
"keyToString": {
|
||||||
"ASKED_ADD_EXTERNAL_FILES": "true",
|
"ASKED_ADD_EXTERNAL_FILES": "true",
|
||||||
"ASKED_MARK_IGNORED_FILES_AS_EXCLUDED": "true",
|
"ASKED_MARK_IGNORED_FILES_AS_EXCLUDED": "true",
|
||||||
"Python.12.executor": "Run",
|
"Python.12.executor": "Run",
|
||||||
"Python.123.executor": "Run",
|
"Python.123.executor": "Run",
|
||||||
"Python.Main.executor": "Run",
|
"Python.Main.executor": "Run",
|
||||||
"Python.tidevice_entry.executor": "Run",
|
"Python.Test.executor": "Run",
|
||||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
"Python.tidevice_entry.executor": "Run",
|
||||||
"RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager.252": "true",
|
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||||
"RunOnceActivity.git.unshallow": "true",
|
"RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager.252": "true",
|
||||||
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
|
"RunOnceActivity.git.unshallow": "true",
|
||||||
"git-widget-placeholder": "main",
|
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
|
||||||
"javascript.nodejs.core.library.configured.version": "20.17.0",
|
"git-widget-placeholder": "main",
|
||||||
"javascript.nodejs.core.library.typings.version": "20.17.58",
|
"javascript.nodejs.core.library.configured.version": "20.17.0",
|
||||||
"last_opened_file_path": "F:/company code/AI item/20250820/iOSAI",
|
"javascript.nodejs.core.library.typings.version": "20.17.58",
|
||||||
"node.js.detected.package.eslint": "true",
|
"last_opened_file_path": "F:/company code/AI item/20250820/iOSAI",
|
||||||
"node.js.detected.package.tslint": "true",
|
"node.js.detected.package.eslint": "true",
|
||||||
"node.js.selected.package.eslint": "(autodetect)",
|
"node.js.detected.package.tslint": "true",
|
||||||
"node.js.selected.package.tslint": "(autodetect)",
|
"node.js.selected.package.eslint": "(autodetect)",
|
||||||
"nodejs_package_manager_path": "npm",
|
"node.js.selected.package.tslint": "(autodetect)",
|
||||||
"settings.editor.selected.configurable": "preferences.editor.code.editing",
|
"nodejs_package_manager_path": "npm",
|
||||||
"vue.rearranger.settings.migration": "true"
|
"settings.editor.selected.configurable": "preferences.editor.code.editing",
|
||||||
|
"vue.rearranger.settings.migration": "true"
|
||||||
}
|
}
|
||||||
}</component>
|
}]]></component>
|
||||||
<component name="RecentsManager">
|
<component name="RecentsManager">
|
||||||
<key name="MoveFile.RECENT_KEYS">
|
<key name="MoveFile.RECENT_KEYS">
|
||||||
<recent name="E:\Code\python\iOSAI\resources" />
|
<recent name="E:\Code\python\iOSAI\resources" />
|
||||||
@@ -165,8 +167,31 @@
|
|||||||
<option name="INPUT_FILE" value="" />
|
<option name="INPUT_FILE" value="" />
|
||||||
<method v="2" />
|
<method v="2" />
|
||||||
</configuration>
|
</configuration>
|
||||||
|
<configuration name="Test" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
|
||||||
|
<module name="iOSAI" />
|
||||||
|
<option name="ENV_FILES" value="" />
|
||||||
|
<option name="INTERPRETER_OPTIONS" value="" />
|
||||||
|
<option name="PARENT_ENVS" value="true" />
|
||||||
|
<envs>
|
||||||
|
<env name="PYTHONUNBUFFERED" value="1" />
|
||||||
|
</envs>
|
||||||
|
<option name="SDK_HOME" value="" />
|
||||||
|
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/Utils" />
|
||||||
|
<option name="IS_MODULE_SDK" value="true" />
|
||||||
|
<option name="ADD_CONTENT_ROOTS" value="true" />
|
||||||
|
<option name="ADD_SOURCE_ROOTS" value="true" />
|
||||||
|
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/Utils/Test.py" />
|
||||||
|
<option name="PARAMETERS" value="" />
|
||||||
|
<option name="SHOW_COMMAND_LINE" value="false" />
|
||||||
|
<option name="EMULATE_TERMINAL" value="false" />
|
||||||
|
<option name="MODULE_MODE" value="false" />
|
||||||
|
<option name="REDIRECT_INPUT" value="false" />
|
||||||
|
<option name="INPUT_FILE" value="" />
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
<recent_temporary>
|
<recent_temporary>
|
||||||
<list>
|
<list>
|
||||||
|
<item itemvalue="Python.Test" />
|
||||||
<item itemvalue="Python.12" />
|
<item itemvalue="Python.12" />
|
||||||
<item itemvalue="Python.123" />
|
<item itemvalue="Python.123" />
|
||||||
</list>
|
</list>
|
||||||
|
|||||||
@@ -27,104 +27,150 @@ class FlaskSubprocessManager:
|
|||||||
self.process: Optional[subprocess.Popen] = None
|
self.process: Optional[subprocess.Popen] = None
|
||||||
self.comm_port = 34566
|
self.comm_port = 34566
|
||||||
self._stop_event = threading.Event()
|
self._stop_event = threading.Event()
|
||||||
|
self._monitor_thread: Optional[threading.Thread] = None
|
||||||
atexit.register(self.stop)
|
atexit.register(self.stop)
|
||||||
|
LogManager.info("FlaskSubprocessManager 单例已初始化", udid="system")
|
||||||
|
|
||||||
# 可以把 _find_available_port 留着备用,但 start 前先校验端口是否被占用
|
# ---------- 启动 ----------
|
||||||
|
def start(self):
|
||||||
|
with self._lock:
|
||||||
|
if self._is_alive():
|
||||||
|
LogManager.warning("子进程已在运行,无需重复启动", udid="system")
|
||||||
|
return
|
||||||
|
|
||||||
|
env = os.environ.copy()
|
||||||
|
env["FLASK_COMM_PORT"] = str(self.comm_port)
|
||||||
|
|
||||||
|
exe_path = Path(sys.executable).resolve()
|
||||||
|
if exe_path.name.lower() in ("python.exe", "pythonw.exe"):
|
||||||
|
exe_path = Path(sys.argv[0]).resolve()
|
||||||
|
is_frozen = exe_path.suffix.lower() == ".exe" and exe_path.exists()
|
||||||
|
|
||||||
|
if is_frozen:
|
||||||
|
cmd = [str(exe_path), "--role=flask"]
|
||||||
|
cwd = str(exe_path.parent)
|
||||||
|
else:
|
||||||
|
cmd = [sys.executable, "-u", "-m", "Module.Main", "--role=flask"]
|
||||||
|
cwd = str(Path(__file__).resolve().parent)
|
||||||
|
|
||||||
|
LogManager.info(f"准备启动 Flask 子进程: {cmd} cwd={cwd}", udid="system")
|
||||||
|
|
||||||
|
# 关键:不再自己 open 文件,直接走 LogManager
|
||||||
|
# 用 PIPE 捕获,再转存到 system 级日志
|
||||||
|
self.process = subprocess.Popen(
|
||||||
|
cmd,
|
||||||
|
stdin=subprocess.DEVNULL,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.STDOUT,
|
||||||
|
text=True,
|
||||||
|
encoding="utf-8",
|
||||||
|
errors="replace",
|
||||||
|
bufsize=1,
|
||||||
|
env=env,
|
||||||
|
cwd=cwd,
|
||||||
|
start_new_session=True
|
||||||
|
)
|
||||||
|
|
||||||
|
# 守护线程:把子进程 stdout → LogManager.info/system
|
||||||
|
threading.Thread(target=self._flush_stdout, daemon=True).start()
|
||||||
|
|
||||||
|
LogManager.info(f"Flask 子进程已启动,PID={self.process.pid},端口={self.comm_port}", udid="system")
|
||||||
|
|
||||||
|
if not self._wait_port_open(timeout=10):
|
||||||
|
LogManager.error("等待端口监听超时,启动失败", udid="system")
|
||||||
|
self.stop()
|
||||||
|
raise RuntimeError("Flask 启动后 10 s 内未监听端口")
|
||||||
|
|
||||||
|
self._monitor_thread = threading.Thread(target=self._monitor, daemon=True)
|
||||||
|
self._monitor_thread.start()
|
||||||
|
LogManager.info("端口守护线程已启动", udid="system")
|
||||||
|
|
||||||
|
# ---------- 实时把子进程 stdout 刷到 system 日志 ----------
|
||||||
|
def _flush_stdout(self):
|
||||||
|
for line in iter(self.process.stdout.readline, ""):
|
||||||
|
if line:
|
||||||
|
LogManager.info(line.rstrip(), udid="system")
|
||||||
|
self.process.stdout.close()
|
||||||
|
|
||||||
|
# ---------- 发送 ----------
|
||||||
|
def send(self, data: Union[str, Dict, List]) -> bool:
|
||||||
|
if isinstance(data, (dict, list)):
|
||||||
|
data = json.dumps(data, ensure_ascii=False)
|
||||||
|
try:
|
||||||
|
with socket.create_connection(("127.0.0.1", self.comm_port), timeout=3.0) as s:
|
||||||
|
s.sendall((data + "\n").encode("utf-8"))
|
||||||
|
LogManager.info(f"数据已成功发送到 Flask 端口:{self.comm_port}", udid="system")
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
LogManager.error(f"发送失败:{e}", udid="system")
|
||||||
|
return False
|
||||||
|
|
||||||
|
# ---------- 停止 ----------
|
||||||
|
def stop(self):
|
||||||
|
with self._lock:
|
||||||
|
if getattr(self, 'process', None) is None:
|
||||||
|
LogManager.info("无子进程需要停止", udid="system")
|
||||||
|
return
|
||||||
|
|
||||||
|
pid = self.process.pid
|
||||||
|
LogManager.info(f"正在停止 Flask 子进程 PID={pid}", udid="system")
|
||||||
|
try:
|
||||||
|
self.process.terminate()
|
||||||
|
try:
|
||||||
|
self.process.wait(timeout=3)
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
LogManager.warning("软杀超时,强制杀进程树", udid="system")
|
||||||
|
import psutil
|
||||||
|
parent = psutil.Process(pid)
|
||||||
|
for child in parent.children(recursive=True):
|
||||||
|
child.kill()
|
||||||
|
parent.kill()
|
||||||
|
self.process.wait()
|
||||||
|
LogManager.info("Flask 子进程已停止", udid="system")
|
||||||
|
except Exception as e:
|
||||||
|
LogManager.error(f"停止子进程时异常:{e}", udid="system")
|
||||||
|
finally:
|
||||||
|
self.process = None
|
||||||
|
self._stop_event.set()
|
||||||
|
|
||||||
|
# ---------- 端口守护 ----------
|
||||||
|
def _monitor(self):
|
||||||
|
LogManager.info("守护线程开始运行,周期性检查端口存活", udid="system")
|
||||||
|
while not self._stop_event.wait(1.0):
|
||||||
|
if not self._port_alive():
|
||||||
|
LogManager.error("检测到端口不通,准备重启 Flask", udid="system")
|
||||||
|
with self._lock:
|
||||||
|
if self.process and self.process.poll() is None:
|
||||||
|
self.stop()
|
||||||
|
try:
|
||||||
|
self.start()
|
||||||
|
except Exception as e:
|
||||||
|
LogManager.error(f"自动重启失败:{e}", udid="system")
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
|
# ---------- 辅助 ----------
|
||||||
def _is_port_busy(self, port: int) -> bool:
|
def _is_port_busy(self, port: int) -> bool:
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||||
s.settimeout(0.2)
|
s.settimeout(0.2)
|
||||||
return s.connect_ex(("127.0.0.1", port)) == 0
|
return s.connect_ex(("127.0.0.1", port)) == 0
|
||||||
|
|
||||||
# 启动flask
|
def _port_alive(self) -> bool:
|
||||||
def start(self):
|
|
||||||
"""启动 Flask 子进程(兼容打包后的 exe 和源码运行)"""
|
|
||||||
with self._lock:
|
|
||||||
if self.process is not None:
|
|
||||||
LogManager.warning("子进程正在运行中!")
|
|
||||||
raise RuntimeError("子进程已在运行中!")
|
|
||||||
|
|
||||||
env = os.environ.copy()
|
|
||||||
env["FLASK_COMM_PORT"] = str(self.comm_port)
|
|
||||||
|
|
||||||
# —— 解析打包 exe 的稳健写法 ——
|
|
||||||
exe_path = Path(sys.executable).resolve()
|
|
||||||
if exe_path.name.lower() in ("python.exe", "pythonw.exe"):
|
|
||||||
# Nuitka 某些场景里 sys.executable 可能指向 dist\python.exe(并不存在)
|
|
||||||
exe_path = Path(sys.argv[0]).resolve()
|
|
||||||
|
|
||||||
is_frozen = exe_path.suffix.lower() == ".exe" and exe_path.exists()
|
|
||||||
|
|
||||||
if is_frozen:
|
|
||||||
# 打包后的 exe:用当前 exe 自举
|
|
||||||
cmd = [str(exe_path), "--role=flask"]
|
|
||||||
cwd = str(exe_path.parent)
|
|
||||||
else:
|
|
||||||
# 源码运行:模块方式更稳
|
|
||||||
cmd = [sys.executable, "-m", "Module.Main", "--role=flask"]
|
|
||||||
cwd = str(Path(__file__).resolve().parent) # Module 目录
|
|
||||||
|
|
||||||
LogManager.info(f"[DEBUG] spawn: {cmd} (cwd={cwd}) exists(exe)={os.path.exists(cmd[0])}")
|
|
||||||
print(f"[DEBUG] spawn: {cmd} (cwd={cwd}) exists(exe)={os.path.exists(cmd[0])}")
|
|
||||||
|
|
||||||
self.process = subprocess.Popen(
|
|
||||||
cmd,
|
|
||||||
stdin=subprocess.PIPE,
|
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
stderr=subprocess.PIPE,
|
|
||||||
text=True,
|
|
||||||
encoding="utf-8",
|
|
||||||
errors="replace", # 新增:遇到非 UTF-8 字节用 <20> 代替,避免崩溃
|
|
||||||
bufsize=1,
|
|
||||||
env=env,
|
|
||||||
cwd=cwd,
|
|
||||||
)
|
|
||||||
|
|
||||||
LogManager.info(f"Flask子进程启动 (PID: {self.process.pid}, 端口: {self.comm_port})")
|
|
||||||
print(f"Flask子进程启动 (PID: {self.process.pid}, 端口: {self.comm_port})")
|
|
||||||
|
|
||||||
def print_output(stream, stream_name):
|
|
||||||
while True:
|
|
||||||
line = stream.readline()
|
|
||||||
if not line:
|
|
||||||
break
|
|
||||||
print(f"{stream_name}: {line.strip()}")
|
|
||||||
|
|
||||||
threading.Thread(target=print_output, args=(self.process.stdout, "STDOUT"), daemon=True).start()
|
|
||||||
threading.Thread(target=print_output, args=(self.process.stderr, "STDERR"), daemon=True).start()
|
|
||||||
|
|
||||||
def send(self, data: Union[str, Dict, List]) -> bool:
|
|
||||||
"""通过Socket发送数据"""
|
|
||||||
try:
|
try:
|
||||||
if not isinstance(data, str):
|
with socket.create_connection(("127.0.0.1", self.comm_port), timeout=0.5):
|
||||||
data = json.dumps(data)
|
return True
|
||||||
# 等待子进程启动并准备好
|
except Exception:
|
||||||
time.sleep(1) # 延时1秒,根据实际情况调整
|
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
||||||
s.connect(('127.0.0.1', self.comm_port))
|
|
||||||
s.sendall((data + "\n").encode('utf-8'))
|
|
||||||
return True
|
|
||||||
except ConnectionRefusedError:
|
|
||||||
LogManager.error(f"连接被拒绝,确保子进程在端口 {self.comm_port} 上监听")
|
|
||||||
print(f"连接被拒绝,确保子进程在端口 {self.comm_port} 上监听")
|
|
||||||
return False
|
|
||||||
except Exception as e:
|
|
||||||
LogManager.error(f"发送失败: {e}")
|
|
||||||
print(f"发送失败: {e}")
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def stop(self):
|
def _wait_port_open(self, timeout: float) -> bool:
|
||||||
with self._lock:
|
t0 = time.time()
|
||||||
if self.process and self.process.poll() is None:
|
while time.time() - t0 < timeout:
|
||||||
print(f"[INFO] Stopping Flask child process (PID: {self.process.pid})...")
|
if self._port_alive():
|
||||||
LogManager.info(f"[INFO] Stopping Flask child process (PID: {self.process.pid})...")
|
return True
|
||||||
self.process.terminate()
|
time.sleep(0.2)
|
||||||
self.process.wait()
|
return False
|
||||||
print("[INFO] Flask child process stopped.")
|
|
||||||
LogManager.info("[INFO] Flask child process stopped.")
|
def _is_alive(self) -> bool:
|
||||||
self._stop_event.set()
|
return self.process is not None and self.process.poll() is None and self._port_alive()
|
||||||
else:
|
|
||||||
LogManager.info("[INFO] No Flask child process to stop.")
|
|
||||||
print("[INFO] No Flask child process to stop.")
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_instance(cls) -> 'FlaskSubprocessManager':
|
def get_instance(cls) -> 'FlaskSubprocessManager':
|
||||||
|
|||||||
@@ -88,8 +88,8 @@ class DevDiskImageDeployer:
|
|||||||
exists = dst.exists()
|
exists = dst.exists()
|
||||||
if exists and not self.overwrite:
|
if exists and not self.overwrite:
|
||||||
skipped += 1
|
skipped += 1
|
||||||
if self.verbose:
|
# if self.verbose:
|
||||||
print(f"[SKIP] {dst} 已存在(目录)")
|
# print(f"[SKIP] {dst} 已存在(目录)")
|
||||||
continue
|
continue
|
||||||
if exists and self.overwrite and not self.dry_run:
|
if exists and self.overwrite and not self.dry_run:
|
||||||
shutil.rmtree(dst)
|
shutil.rmtree(dst)
|
||||||
@@ -105,8 +105,8 @@ class DevDiskImageDeployer:
|
|||||||
exists = dst.exists()
|
exists = dst.exists()
|
||||||
if exists and not self.overwrite:
|
if exists and not self.overwrite:
|
||||||
skipped += 1
|
skipped += 1
|
||||||
if self.verbose:
|
# if self.verbose:
|
||||||
print(f"[SKIP] {dst} 已存在(zip)")
|
# print(f"[SKIP] {dst} 已存在(zip)")
|
||||||
continue
|
continue
|
||||||
if exists and self.overwrite and not self.dry_run:
|
if exists and self.overwrite and not self.dry_run:
|
||||||
dst.unlink()
|
dst.unlink()
|
||||||
|
|||||||
Reference in New Issue
Block a user