perf: 优化任务执行速度 (40-70s → ~15s)

问题:容错机制引入了大量叠加的等待时间

优化内容:
- playwright_automation.py:
  - 登录超时 30s → 10s
  - 导航等待 2s → 0.5s
  - navigate_only 等待 1s → 0.3s
  - 首页轮询 8次×3s → networkidle + 2次×0.5s
- services/tasks.py:
  - 删除截图前固定 sleep(2)
- services/screenshots.py:
  - networkidle 超时 30s → 10s
  - selector 超时 20s → 5s

预计性能提升:从 40-70 秒降至约 15 秒

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-24 00:19:46 +08:00
parent 79a571e58d
commit 1d44859857
3 changed files with 16 additions and 23 deletions

View File

@@ -424,7 +424,7 @@ class PlaywrightAutomation:
# 等待跳转
# self.log("等待登录处理...") # 精简日志
self.page.wait_for_load_state('networkidle', timeout=30000) # 增加到30秒
self.page.wait_for_load_state('networkidle', timeout=10000) # 优化为10秒
# 检查登录结果
current_url = self.page.url
@@ -823,7 +823,7 @@ class PlaywrightAutomation:
self.log(f"导航到 '{browse_type}' 页面...")
try:
# 等待页面完全加载
time.sleep(2)
time.sleep(0.5)
self.log(f"当前URL: {self.main_page.url}")
except Exception as e:
self.log(f"获取URL失败: {str(e)}")
@@ -835,7 +835,7 @@ class PlaywrightAutomation:
# 如果只是导航(用于截图),切换完成后直接返回
if navigate_only:
time.sleep(1) # 等待页面稳定
time.sleep(0.3) # 等待页面稳定
result.success = True
return result
@@ -867,27 +867,21 @@ class PlaywrightAutomation:
except Exception: # Bug fix: 明确捕获Exception
self.log("等待表格超时,继续尝试...")
# 额外等待确保AJAX内容加载完成
# 第一页等待更长时间因为是首次加载并发时尤其<E5B0A4><E585B6><EFBFBD>
if current_page == 1 and total_items == 0:
time.sleep(3.0)
else:
time.sleep(1.0)
# 等待页面网络空闲确保AJAX加载完成
try:
self.page.wait_for_load_state('networkidle', timeout=5000)
except Exception:
pass # 超时继续,不阻塞
# 获取内容行数量(带重试机制避免AJAX加载慢导致误判
# 第一页使用更多重试次数8次×3秒=24秒处理高并发时的慢加载
# 后续页使用3次×1.5秒=4.5秒
max_retries = 8 if (current_page == 1 and total_items == 0) else 3
retry_wait = 3.0 if (current_page == 1 and total_items == 0) else 1.5
# 获取内容行数量(简化重试2次快速检测
rows_count = 0
for retry in range(max_retries):
for retry in range(2):
rows_locator = self.page.locator("//table[@class='ltable']/tbody/tr[position()>1 and count(td)>=5]")
rows_count = rows_locator.count()
if rows_count > 0:
break
if retry < max_retries - 1:
self.log(f"未检测到内容,等待后重试... ({retry+1}/{max_retries})")
time.sleep(retry_wait)
if retry == 0:
time.sleep(0.5) # 仅重试一次等待0.5秒
if rows_count == 0:
self.log("当前页面没有内容")

View File

@@ -130,11 +130,11 @@ def take_screenshot_for_account(
if "center.aspx" not in current_url:
raise RuntimeError(f"unexpected_iframe_url:{current_url}")
try:
iframe.wait_for_load_state("networkidle", timeout=30000)
iframe.wait_for_load_state("networkidle", timeout=10000)
except Exception:
pass
try:
iframe.wait_for_selector("table.ltable", timeout=20000)
iframe.wait_for_selector("table.ltable", timeout=5000)
except Exception:
pass
else:
@@ -144,11 +144,11 @@ def take_screenshot_for_account(
if "center.aspx" not in current_url:
raise RuntimeError(f"unexpected_url:{current_url}")
try:
automation.main_page.wait_for_load_state("networkidle", timeout=30000)
automation.main_page.wait_for_load_state("networkidle", timeout=10000)
except Exception:
pass
try:
automation.main_page.wait_for_selector("table.ltable", timeout=20000)
automation.main_page.wait_for_selector("table.ltable", timeout=5000)
except Exception:
pass
navigated = True

View File

@@ -857,7 +857,6 @@ def run_task(user_id, account_id, browse_type, enable_screenshot=True, source="m
},
},
)
time.sleep(2)
browse_result_dict = {"total_items": result.total_items, "total_attachments": result.total_attachments}
screenshot_submitted = True
threading.Thread(