From 1d44859857eb0d0fbf3f5c828d7ca5b6a20c3434 Mon Sep 17 00:00:00 2001 From: yuyx <237899745@qq.com> Date: Wed, 24 Dec 2025 00:19:46 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E9=80=9F=E5=BA=A6=20(40-70s=20=E2=86=92=20~1?= =?UTF-8?q?5s)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题:容错机制引入了大量叠加的等待时间 优化内容: - 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 --- playwright_automation.py | 30 ++++++++++++------------------ services/screenshots.py | 8 ++++---- services/tasks.py | 1 - 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/playwright_automation.py b/playwright_automation.py index cefcbaf..7c2d9d5 100755 --- a/playwright_automation.py +++ b/playwright_automation.py @@ -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内容加载完成 - # 第一页等待更长时间,因为是首次加载(并发时尤其���要) - 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("当前页面没有内容") diff --git a/services/screenshots.py b/services/screenshots.py index 2a58698..a66cefd 100644 --- a/services/screenshots.py +++ b/services/screenshots.py @@ -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 diff --git a/services/tasks.py b/services/tasks.py index fa0a75a..717e54a 100644 --- a/services/tasks.py +++ b/services/tasks.py @@ -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(