Skip to content

Commit 558a607

Browse files
authored
perf(playwright): 优化启动内存(更新框架无用,需要去插件时长重装) (#51)
1 parent 320f3fe commit 558a607

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

modules/playwright/main.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@
6060
'--disable-software-rasterizer',
6161
'--disable-extensions',
6262
'--disable-background-networking',
63+
# 省内存: 去掉 zygote 预热进程 + 限制 renderer 进程数 + 关闭进程级站点隔离,
64+
# 常驻 chromium 空闲 RSS 约 -80MB、少 2 个进程 (本地自包含 HTML 渲染无副作用)。
65+
'--no-zygote',
66+
'--renderer-process-limit=1',
67+
'--disable-features=site-per-process,TranslateUI',
68+
'--disable-accelerated-2d-canvas',
69+
'--disable-background-timer-throttling',
70+
'--mute-audio',
6371
],
6472
}
6573

@@ -320,6 +328,7 @@ async def screenshot_html(
320328
wait_ms=0,
321329
selector=None,
322330
base_url=None,
331+
wait_until='load',
323332
):
324333
"""截图 HTML 字符串, 返回图片 bytes
325334
@@ -332,6 +341,9 @@ async def screenshot_html(
332341
wait_ms — set_content 后额外等待毫秒
333342
selector — CSS 选择器, 指定则只截取该元素
334343
base_url — HTML 中相对路径的基础 URL
344+
wait_until — set_content 等待策略, 默认 'load' (自包含 HTML 用
345+
'networkidle' 会白等 ~500ms 空闲窗口; 含外链懒加载资源
346+
时才需要传 'networkidle')
335347
"""
336348
fmt = image_format or self._cfg.get('image_format', 'jpeg')
337349
q = quality or self._cfg.get('image_quality', 90)
@@ -340,7 +352,7 @@ async def screenshot_html(
340352
kw = {}
341353
if base_url:
342354
kw['base_url'] = base_url
343-
await page.set_content(html, wait_until='networkidle', **kw)
355+
await page.set_content(html, wait_until=wait_until, **kw)
344356
if wait_ms > 0:
345357
await page.wait_for_timeout(wait_ms)
346358
return await self._take_screenshot(page, full_page, fmt, q, selector)

0 commit comments

Comments
 (0)