继续优化批量停止脚本

This commit is contained in:
2025-09-20 20:07:16 +08:00
parent a4effb8058
commit 68b329f4f9
7 changed files with 122 additions and 39 deletions

View File

@@ -90,16 +90,23 @@ class ThreadManager:
# ---------- 2. 并发等 0.2 s 收尾 ----------
def _wait_and_clean(udid: str) -> Tuple[int, str]:
"""子线程里只做极短 join 并清理记录"""
with cls._lock:
task = cls._tasks.get(udid)
if not task:
return 400, f"设备{udid}任务记录已丢失"
return 400, "任务记录已丢失"
thread = task["thread"]
# 浅等 0.2 s后台还没死也继续
thread.join(0.2)
del cls._tasks[udid] # 立即清理
return 200, f"设备{udid}已下发停止指令"
# 第一次等 3 秒,让“分片睡眠”有机会退出
thread.join(timeout=3)
# 如果还活,再补 2 秒
if thread.is_alive():
thread.join(timeout=2)
# 最终仍活,记录日志但不硬杀,避免僵尸
with cls._lock:
cls._tasks.pop(udid, None)
if thread.is_alive():
LogManager.warning(f"[batch_stop] 线程 5s 未退出,已清理记录但线程仍跑 {udid}")
return 201, "已下发停止,线程超长任务未立即结束"
return 200, "已停止"
with ThreadPoolExecutor(max_workers=min(32, len(udids))) as executor:
future_map = {executor.submit(_wait_and_clean, udid): udid for udid in udids}