修复bug。临时提交
This commit is contained in:
@@ -9,6 +9,9 @@ import time
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union, Dict, List
|
||||
|
||||
from Utils.LogManager import LogManager
|
||||
|
||||
|
||||
class FlaskSubprocessManager:
|
||||
_instance: Optional['FlaskSubprocessManager'] = None
|
||||
_lock: threading.Lock = threading.Lock()
|
||||
@@ -22,7 +25,7 @@ class FlaskSubprocessManager:
|
||||
|
||||
def _init_manager(self):
|
||||
self.process: Optional[subprocess.Popen] = None
|
||||
self.comm_port = 34567
|
||||
self.comm_port = 34566
|
||||
self._stop_event = threading.Event()
|
||||
atexit.register(self.stop)
|
||||
|
||||
@@ -37,6 +40,7 @@ class FlaskSubprocessManager:
|
||||
"""启动 Flask 子进程(兼容打包后的 exe 和源码运行)"""
|
||||
with self._lock:
|
||||
if self.process is not None:
|
||||
LogManager.warning("子进程正在运行中!")
|
||||
raise RuntimeError("子进程已在运行中!")
|
||||
|
||||
env = os.environ.copy()
|
||||
@@ -59,6 +63,7 @@ class FlaskSubprocessManager:
|
||||
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(
|
||||
@@ -73,6 +78,8 @@ class FlaskSubprocessManager:
|
||||
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):
|
||||
@@ -97,9 +104,11 @@ class FlaskSubprocessManager:
|
||||
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
|
||||
|
||||
@@ -107,11 +116,14 @@ class FlaskSubprocessManager:
|
||||
with self._lock:
|
||||
if self.process and self.process.poll() is None:
|
||||
print(f"[INFO] Stopping Flask child process (PID: {self.process.pid})...")
|
||||
LogManager.info(f"[INFO] Stopping Flask child process (PID: {self.process.pid})...")
|
||||
self.process.terminate()
|
||||
self.process.wait()
|
||||
print("[INFO] Flask child process stopped.")
|
||||
LogManager.info("[INFO] Flask child process stopped.")
|
||||
self._stop_event.set()
|
||||
else:
|
||||
LogManager.info("[INFO] No Flask child process to stop.")
|
||||
print("[INFO] No Flask child process to stop.")
|
||||
|
||||
@classmethod
|
||||
|
||||
Reference in New Issue
Block a user