ts
This commit is contained in:
+9
-7
@@ -72,13 +72,15 @@ templates = Jinja2Templates(directory=os.path.join(config.BASE_DIR, "templates")
|
||||
|
||||
@app.on_event("startup")
|
||||
async def on_startup():
|
||||
# 启动 Playwright + Browser(长期复用,减少反复启动带来的抖动)
|
||||
app_state.playwright = await async_playwright().start()
|
||||
app_state.browser = await app_state.playwright.chromium.launch(
|
||||
headless=config.HEADLESS,
|
||||
args=config.BROWSER_ARGS,
|
||||
)
|
||||
log_manager.append("INFO", f"Browser launched (headless={config.HEADLESS})")
|
||||
"""
|
||||
只做轻量初始化,不在启动时强制拉起浏览器。
|
||||
原因:
|
||||
- 很多环境首次运行未执行 `playwright install chromium`,如果启动即 launch,会导致 Web 直接起不来(你看到的“打不开”)。
|
||||
- 浏览器改为按需启动:第一次需要登录/解析/采集时再启动,失败也能在 UI 里看到明确错误。
|
||||
"""
|
||||
app_state.playwright = None
|
||||
app_state.browser = None
|
||||
log_manager.append("INFO", "App started (browser will be launched lazily on demand)")
|
||||
|
||||
|
||||
@app.on_event("shutdown")
|
||||
|
||||
Reference in New Issue
Block a user