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:
@@ -424,7 +424,7 @@ class PlaywrightAutomation:
|
|||||||
|
|
||||||
# 等待跳转
|
# 等待跳转
|
||||||
# self.log("等待登录处理...") # 精简日志
|
# 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
|
current_url = self.page.url
|
||||||
@@ -823,7 +823,7 @@ class PlaywrightAutomation:
|
|||||||
self.log(f"导航到 '{browse_type}' 页面...")
|
self.log(f"导航到 '{browse_type}' 页面...")
|
||||||
try:
|
try:
|
||||||
# 等待页面完全加载
|
# 等待页面完全加载
|
||||||
time.sleep(2)
|
time.sleep(0.5)
|
||||||
self.log(f"当前URL: {self.main_page.url}")
|
self.log(f"当前URL: {self.main_page.url}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.log(f"获取URL失败: {str(e)}")
|
self.log(f"获取URL失败: {str(e)}")
|
||||||
@@ -835,7 +835,7 @@ class PlaywrightAutomation:
|
|||||||
|
|
||||||
# 如果只是导航(用于截图),切换完成后直接返回
|
# 如果只是导航(用于截图),切换完成后直接返回
|
||||||
if navigate_only:
|
if navigate_only:
|
||||||
time.sleep(1) # 等待页面稳定
|
time.sleep(0.3) # 等待页面稳定
|
||||||
result.success = True
|
result.success = True
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@@ -867,27 +867,21 @@ class PlaywrightAutomation:
|
|||||||
except Exception: # Bug fix: 明确捕获Exception
|
except Exception: # Bug fix: 明确捕获Exception
|
||||||
self.log("等待表格超时,继续尝试...")
|
self.log("等待表格超时,继续尝试...")
|
||||||
|
|
||||||
# 额外等待,确保AJAX内容加载完成
|
# 等待页面网络空闲,确保AJAX加载完成
|
||||||
# 第一页等待更长时间,因为是首次加载(并发时尤其<E5B0A4><E585B6><EFBFBD>要)
|
try:
|
||||||
if current_page == 1 and total_items == 0:
|
self.page.wait_for_load_state('networkidle', timeout=5000)
|
||||||
time.sleep(3.0)
|
except Exception:
|
||||||
else:
|
pass # 超时继续,不阻塞
|
||||||
time.sleep(1.0)
|
|
||||||
|
|
||||||
# 获取内容行数量(带重试机制,避免AJAX加载慢导致误判)
|
# 获取内容行数量(简化重试:2次快速检测)
|
||||||
# 第一页使用更多重试次数(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
|
|
||||||
rows_count = 0
|
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_locator = self.page.locator("//table[@class='ltable']/tbody/tr[position()>1 and count(td)>=5]")
|
||||||
rows_count = rows_locator.count()
|
rows_count = rows_locator.count()
|
||||||
if rows_count > 0:
|
if rows_count > 0:
|
||||||
break
|
break
|
||||||
if retry < max_retries - 1:
|
if retry == 0:
|
||||||
self.log(f"未检测到内容,等待后重试... ({retry+1}/{max_retries})")
|
time.sleep(0.5) # 仅重试一次,等待0.5秒
|
||||||
time.sleep(retry_wait)
|
|
||||||
|
|
||||||
if rows_count == 0:
|
if rows_count == 0:
|
||||||
self.log("当前页面没有内容")
|
self.log("当前页面没有内容")
|
||||||
|
|||||||
@@ -130,11 +130,11 @@ def take_screenshot_for_account(
|
|||||||
if "center.aspx" not in current_url:
|
if "center.aspx" not in current_url:
|
||||||
raise RuntimeError(f"unexpected_iframe_url:{current_url}")
|
raise RuntimeError(f"unexpected_iframe_url:{current_url}")
|
||||||
try:
|
try:
|
||||||
iframe.wait_for_load_state("networkidle", timeout=30000)
|
iframe.wait_for_load_state("networkidle", timeout=10000)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
iframe.wait_for_selector("table.ltable", timeout=20000)
|
iframe.wait_for_selector("table.ltable", timeout=5000)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
@@ -144,11 +144,11 @@ def take_screenshot_for_account(
|
|||||||
if "center.aspx" not in current_url:
|
if "center.aspx" not in current_url:
|
||||||
raise RuntimeError(f"unexpected_url:{current_url}")
|
raise RuntimeError(f"unexpected_url:{current_url}")
|
||||||
try:
|
try:
|
||||||
automation.main_page.wait_for_load_state("networkidle", timeout=30000)
|
automation.main_page.wait_for_load_state("networkidle", timeout=10000)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
automation.main_page.wait_for_selector("table.ltable", timeout=20000)
|
automation.main_page.wait_for_selector("table.ltable", timeout=5000)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
navigated = True
|
navigated = True
|
||||||
|
|||||||
@@ -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}
|
browse_result_dict = {"total_items": result.total_items, "total_attachments": result.total_attachments}
|
||||||
screenshot_submitted = True
|
screenshot_submitted = True
|
||||||
threading.Thread(
|
threading.Thread(
|
||||||
|
|||||||
Reference in New Issue
Block a user