fix: avoid blocking browser init

This commit is contained in:
2025-12-18 09:38:02 +08:00
parent 5851120f87
commit 433a3cb806
4 changed files with 108 additions and 17 deletions

View File

@@ -7,6 +7,7 @@ API 浏览器 - 用纯 HTTP 请求实现浏览功能
import requests
from bs4 import BeautifulSoup
import os
import re
import time
import atexit
@@ -24,6 +25,12 @@ LOGIN_URL = getattr(config, "ZSGL_LOGIN_URL", f"{BASE_URL}/admin/login.aspx")
INDEX_URL_PATTERN = getattr(config, "ZSGL_INDEX_URL_PATTERN", "index.aspx")
COOKIES_DIR = getattr(config, "COOKIES_DIR", "data/cookies")
try:
_API_REQUEST_TIMEOUT_SECONDS = float(os.environ.get("API_REQUEST_TIMEOUT_SECONDS") or os.environ.get("API_REQUEST_TIMEOUT") or "15")
except Exception:
_API_REQUEST_TIMEOUT_SECONDS = 15.0
_API_REQUEST_TIMEOUT_SECONDS = max(3.0, _API_REQUEST_TIMEOUT_SECONDS)
_cookie_domain_fallback = urlsplit(BASE_URL).hostname or "postoa.aidunsoft.com"
_api_browser_instances: "weakref.WeakSet[APIBrowser]" = weakref.WeakSet()
@@ -125,7 +132,7 @@ class APIBrowser:
def _request_with_retry(self, method, url, max_retries=3, retry_delay=1, **kwargs):
"""带重试机制的请求方法"""
kwargs.setdefault('timeout', 5)
kwargs.setdefault('timeout', _API_REQUEST_TIMEOUT_SECONDS)
last_error = None
for attempt in range(1, max_retries + 1):