From 81a99e007b22f5e81f9bd4b30243b7a8cb83eec2 Mon Sep 17 00:00:00 2001 From: butubb <1422726308@qq.com> Date: Thu, 20 Aug 2026 09:33:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=AF=E5=8A=A8=E6=97=B6=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E5=B9=B6=E5=8F=91=E9=A2=84=E8=BF=9E=E6=8E=A5=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E6=B1=A0=E2=80=94=E2=80=94adb=20server=20=E9=87=8D?= =?UTF-8?q?=E5=90=AF=E5=90=8E=E8=AE=BE=E5=A4=87=E5=85=A8=E6=8E=89=E7=BA=BF?= =?UTF-8?q?=EF=BC=8Cadb=20=E5=91=BD=E4=BB=A4=E6=8E=92=E9=98=9F=E5=8D=A1?= =?UTF-8?q?=E5=87=A0=E5=88=86=E9=92=9F=EF=BC=9B=E9=A2=84=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=EF=BC=88=E5=B9=B6=E5=8F=91=20connect=20IP:5555=EF=BC=89?= =?UTF-8?q?=E6=8A=8A=E6=81=A2=E5=A4=8D=E6=97=B6=E9=97=B4=E9=99=8D=E5=88=B0?= =?UTF-8?q?=E7=A7=92=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web_server.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/web_server.py b/web_server.py index 3396e2a..c864fc4 100644 --- a/web_server.py +++ b/web_server.py @@ -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)