优化停止脚本,调整停止脚本方案
This commit is contained in:
@@ -68,19 +68,41 @@ class ThreadManager:
|
||||
@classmethod
|
||||
def batch_stop(cls, udids: List[str]) -> Tuple[int, List[str], str]:
|
||||
"""
|
||||
批量停止任务,使用多线程并发执行。
|
||||
:param udids: 待停止的设备唯一标识列表
|
||||
:return: (code, fail_list, msg)
|
||||
code=200 全部成功,fail_list=[]
|
||||
code=1001 部分/全部失败,fail_list 为失败描述字符串列表
|
||||
批量停止任务——瞬间下发停止信号,仍统计失败结果。
|
||||
返回格式与原接口 100% 兼容:
|
||||
(code, fail_list, msg)
|
||||
code=200 全部成功
|
||||
code=1001 部分/全部失败
|
||||
"""
|
||||
if not udids:
|
||||
return 200, [], "无设备需要停止"
|
||||
|
||||
fail_list: List[str] = []
|
||||
|
||||
# ---------- 1. 瞬间置位 event ----------
|
||||
with cls._lock:
|
||||
for udid in udids:
|
||||
task = cls._tasks.get(udid)
|
||||
if not task or not task.get("running"):
|
||||
fail_list.append(f"设备{udid}停止失败:当前设备没有执行相关任务")
|
||||
continue
|
||||
task["event"].set() # 下发停止信号
|
||||
|
||||
# ---------- 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}任务记录已丢失"
|
||||
thread = task["thread"]
|
||||
# 浅等 0.2 s,后台还没死也继续
|
||||
thread.join(0.2)
|
||||
del cls._tasks[udid] # 立即清理
|
||||
return 200, f"设备{udid}已下发停止指令"
|
||||
|
||||
with ThreadPoolExecutor(max_workers=min(32, len(udids))) as executor:
|
||||
future_map = {executor.submit(cls.stop, udid): udid for udid in udids}
|
||||
future_map = {executor.submit(_wait_and_clean, udid): udid for udid in udids}
|
||||
for future in as_completed(future_map):
|
||||
udid = future_map[future]
|
||||
try:
|
||||
@@ -89,8 +111,10 @@ class ThreadManager:
|
||||
fail_list.append(f"设备{udid}停止失败:{reason}")
|
||||
except Exception as exc:
|
||||
fail_list.append(f"设备{udid}停止异常:{exc}")
|
||||
|
||||
# ---------- 3. 返回兼容格式 ----------
|
||||
if fail_list:
|
||||
return 1001, fail_list, "停止失败"
|
||||
return 1001, fail_list, "部分设备停止失败"
|
||||
return 200, [], "全部设备停止成功"
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user