diff --git a/Module/DeviceInfo.py b/Module/DeviceInfo.py index e4ee4f3..c5e4cac 100644 --- a/Module/DeviceInfo.py +++ b/Module/DeviceInfo.py @@ -71,6 +71,17 @@ def _pick_free_port(low: int = 20000, high: int = 48000) -> int: class DeviceInfo: + _instance = None + _instance_lock = threading.Lock() + + def __new__(cls, *args, **kwargs): + # 双重检查锁,确保线程安全单例 + if not cls._instance: + with cls._instance_lock: + if not cls._instance: + cls._instance = super().__new__(cls) + return cls._instance + # ---- 端口分配:加一个最小的“保留池”,避免并发选到同一个端口 ---- def _alloc_port(self) -> int: with self._lock: @@ -105,6 +116,10 @@ class DeviceInfo: WDA_READY_TIMEOUT = float(os.getenv("WDA_READY_TIMEOUT", "35.0")) def __init__(self) -> None: + # 防止多次初始化(因为 __init__ 每次调用 DeviceInfo() 都会执行) + if getattr(self, "_initialized", False): + return + self._lock = threading.RLock() self._models: Dict[str, DeviceModel] = {} self._iproxy: Dict[str, subprocess.Popen] = {} diff --git a/Module/FlaskSubprocessManager.py b/Module/FlaskSubprocessManager.py index ef77105..94b83be 100644 --- a/Module/FlaskSubprocessManager.py +++ b/Module/FlaskSubprocessManager.py @@ -10,7 +10,6 @@ from pathlib import Path from typing import Optional, Union, Dict, List import psutil - from Utils.LogManager import LogManager @@ -173,6 +172,11 @@ class FlaskSubprocessManager: self.stop() try: self.start() + from Module.DeviceInfo import DeviceInfo + # 重新发送设备相关数据到flask + info = DeviceInfo() + for model in info._models.keys(): + self.send(model) except Exception as e: LogManager.error(f"自动重启失败:{e}", udid="system") time.sleep(2) diff --git a/Module/Main.py b/Module/Main.py index 92de576..11b32b7 100644 --- a/Module/Main.py +++ b/Module/Main.py @@ -50,7 +50,7 @@ if __name__ == "__main__": # 清空日志 LogManager.clearLogs() - main(sys.argv) + # main(sys.argv) # 添加iOS开发包到电脑上 deployer = DevDiskImageDeployer(verbose=True) diff --git a/Module/__pycache__/DeviceInfo.cpython-312.pyc b/Module/__pycache__/DeviceInfo.cpython-312.pyc index 7460619..0fc3ecf 100644 Binary files a/Module/__pycache__/DeviceInfo.cpython-312.pyc and b/Module/__pycache__/DeviceInfo.cpython-312.pyc differ diff --git a/Module/__pycache__/FlaskSubprocessManager.cpython-312.pyc b/Module/__pycache__/FlaskSubprocessManager.cpython-312.pyc index ccf64c6..5eb7486 100644 Binary files a/Module/__pycache__/FlaskSubprocessManager.cpython-312.pyc and b/Module/__pycache__/FlaskSubprocessManager.cpython-312.pyc differ diff --git a/Module/__pycache__/Main.cpython-312.pyc b/Module/__pycache__/Main.cpython-312.pyc index c670ff1..686ac39 100644 Binary files a/Module/__pycache__/Main.cpython-312.pyc and b/Module/__pycache__/Main.cpython-312.pyc differ diff --git a/Utils/LogManager.py b/Utils/LogManager.py index 3981133..0c8bb94 100644 --- a/Utils/LogManager.py +++ b/Utils/LogManager.py @@ -26,7 +26,7 @@ def _force_utf8_everywhere(): except Exception: pass -_force_utf8_everywhere() +# _force_utf8_everywhere() class LogManager: """ diff --git a/Utils/__pycache__/ControlUtils.cpython-312.pyc b/Utils/__pycache__/ControlUtils.cpython-312.pyc index 63e2f01..b47f96a 100644 Binary files a/Utils/__pycache__/ControlUtils.cpython-312.pyc and b/Utils/__pycache__/ControlUtils.cpython-312.pyc differ diff --git a/Utils/__pycache__/LogManager.cpython-312.pyc b/Utils/__pycache__/LogManager.cpython-312.pyc index 46aff74..7f2af26 100644 Binary files a/Utils/__pycache__/LogManager.cpython-312.pyc and b/Utils/__pycache__/LogManager.cpython-312.pyc differ diff --git a/script/__pycache__/ScriptManager.cpython-312.pyc b/script/__pycache__/ScriptManager.cpython-312.pyc index 402a034..2585a6e 100644 Binary files a/script/__pycache__/ScriptManager.cpython-312.pyc and b/script/__pycache__/ScriptManager.cpython-312.pyc differ