feat: 启动时后台并发预连接设备池——adb server 重启后设备全掉线,adb 命令排队卡几分钟;预连接(并发 connect IP:5555)把恢复时间降到秒级

This commit is contained in:
2026-08-20 09:33:24 +08:00
parent f78955f119
commit 81a99e007b
+29
View File
@@ -147,6 +147,33 @@ def _stop_uiauto():
_uiauto_proc = None
def _preconnect_pool_devices():
"""容器/服务重启后 adb server 重启,设备全部掉线,逐个重连要几分钟;
启动时后台并发 connect 设备池里的网络设备(IP:5555),把恢复时间降到秒级。
USB 设备无需 connect(adb server 自动重枚举),只连网络设备。
预连接幂等:已连接的设备返回 already connected,无副作用。
"""
def _run():
try:
from core import device_pool
from core.adb_helper import _adb
serials = [s for s in device_pool.list_configured() if ":" in s]
if not serials:
return
_log.info(f"预连接设备池 {len(serials)} 台网络设备(重启后加速恢复)...")
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as ex:
list(ex.map(lambda s: _adb("connect", s), serials))
out = _adb("devices")
online = sum(1 for ln in (out or "").splitlines()[1:]
if len(ln.split()) >= 2 and ln.split()[1] == "device")
_log.info(f"设备池预连接完成: {online} 台在线")
except Exception as e:
_log.warning(f"设备池预连接失败: {e}")
threading.Thread(target=_run, daemon=True).start()
def _run_server(host, port):
"""启动 Flask,自动处理端口冲突和权限问题。
@@ -185,6 +212,8 @@ if __name__ == "__main__":
from config import WEB_HOST, WEB_PORT
# 自动启动 uiautodev 服务(元素抓取功能依赖,端口 20242)
_ensure_uiauto_running()
# 预连接设备池:adb server 重启后设备全掉线,后台并发重连加速恢复
_preconnect_pool_devices()
_log.info(f"管理后台: http://localhost:{WEB_PORT}/ (admin/admin123)")
try:
_run_server(WEB_HOST, WEB_PORT)