From 32eebf3dd1f5ba1fc34cc8f6fcbca9d87d8d7823 Mon Sep 17 00:00:00 2001 From: hz4th_coder Date: Wed, 12 Aug 2026 10:19:19 +0800 Subject: [PATCH] =?UTF-8?q?auto=20=E4=BB=BB=E5=8A=A1=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E4=B8=AD=E5=AE=9E=E6=97=B6=E6=98=BE=E7=A4=BA=E5=B7=B2=E7=88=AC?= =?UTF-8?q?/=E5=BE=85=E7=88=AC=E7=BC=93=E5=AD=98=E6=95=B0;=20=E4=BA=BA?= =?UTF-8?q?=E5=B7=A5=E5=81=9C=E6=AD=A2=E5=90=8E=E7=BB=A7=E7=BB=AD=E7=88=AC?= =?UTF-8?q?=E5=8F=96=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - engine: CrawlJob 增加 _auto_visited/_auto_pending 运行中实时状态 (含发现新链接入队后刷新) - app.py 详情接口: 任务运行中优先读内存实时值, 不再显示上次运行结束的旧数字 - 端到端验证: 人工停止(已爬3,缓存18) → 继续爬取(跳过seed,爬完18页,缓存0) ✅ - 运行中采样: visited 1→2 实时增长, pending 15→14 同步递减 ✅ --- app.py | 14 +++++++++++--- engine.py | 8 ++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index e52549b..25da80a 100644 --- a/app.py +++ b/app.py @@ -308,11 +308,19 @@ def api_task_detail(tid): result["run_page"] = page result["run_pages"] = pages result["run_page_size"] = page_size - # auto 状态数量 (独立文件) + # auto 状态数量: 运行中优先读内存实时值, 否则读状态文件 if task.get("mode") == "auto": + live_v = live_p = None + with JOBS_LOCK: + job = JOBS.get(tid) + if job: + live_v = getattr(job, "_auto_visited", None) + live_p = getattr(job, "_auto_pending", None) st = store.load_auto_state(tid, task) - result["auto_pending_count"] = len(st.get("pending", [])) - result["auto_visited_count"] = len(st.get("visited", [])) + result["auto_pending_count"] = ( + live_p if live_p is not None else len(st.get("pending", []))) + result["auto_visited_count"] = ( + live_v if live_v is not None else len(st.get("visited", []))) return jsonify(result) diff --git a/engine.py b/engine.py index 7c64bc4..3214d5a 100644 --- a/engine.py +++ b/engine.py @@ -226,6 +226,9 @@ class CrawlJob: self.persist = persist # callable(task_id, run) self._stop = threading.Event() self._pause = threading.Event() + # 运行中的 auto 实时状态 (供详情接口读取; 任务结束时由状态文件兜底) + self._auto_visited = None + self._auto_pending = None self._cfg_lock = threading.RLock() self.thread = None @@ -630,6 +633,8 @@ class CrawlJob: queued.add(normalize_url(u)) run["progress"]["total"] = len(queue) + self._auto_visited = len(visited) + self._auto_pending = len(queue) self._persist() p, browser, ctx, page, cookie_file = self._open_browser() @@ -650,6 +655,8 @@ class CrawlJob: if key in visited and url != seed: # 起始网址不去重, 其余已爬跳过 continue visited.add(key) + self._auto_visited = len(visited) + self._auto_pending = len(queue) idx = len(visited) run["progress"]["current_url"] = url run["progress"]["done"] = len(visited) @@ -666,6 +673,7 @@ class CrawlJob: if lk not in visited and lk not in queued: queued.add(lk) queue.append((link, depth + 1, url)) + self._auto_pending = len(queue) # 新链接入队后实时刷新 if entry["status"] == "OK": self._delay() run["progress"]["total"] = len(visited)