Skip to content

Commit 3c46089

Browse files
perf: 复用 AsyncClient 替代 ThreadPoolExecutor 避免 SSL 上下文阻塞 (#162)
fix: (#160) ## Summary by Sourcery 增强功能: - 将基于 `ThreadPoolExecutor` 的同步 HTTP 调用替换为可复用的 `httpx.AsyncClient`,以实现对 PyPI 版本的非阻塞检查。 <details> <summary>Original summary in English</summary> ## Summary by Sourcery Enhancements: - Replace ThreadPoolExecutor-based synchronous HTTP calls with a reusable httpx.AsyncClient for non-blocking PyPI version checks. </details> --------- Co-authored-by: weinibuliu <weinibuliu@outlook.com>
1 parent 39b2d6b commit 3c46089

1 file changed

Lines changed: 11 additions & 17 deletions

File tree

  • src/MaaDebugger/utils/update_checker

src/MaaDebugger/utils/update_checker/check.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
from concurrent.futures import ThreadPoolExecutor
32
from enum import auto, Enum
43
from typing import Any, Optional, Union
54

@@ -11,23 +10,21 @@
1110
PYPI_API = "https://pypi.org/pypi/MaaDebugger/json"
1211
TSINGHUA_PYPI_API = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/json/maadebugger"
1312

14-
_executor = ThreadPoolExecutor(max_workers=2)
13+
_client = httpx.AsyncClient()
1514

1615

1716
class CheckStatus(Enum):
1817
FAILED = auto()
1918
SKIPPED = auto()
2019

2120

22-
def _sync_get_from_pypi(url: str) -> Optional[str]: # -> '1.8.0b1'
23-
"""Synchronous version of get_from_pypi, runs in a thread pool."""
21+
async def _get_from_pypi(url: str) -> Optional[str]: # -> '1.8.0b1'
2422
try:
25-
with httpx.Client() as client:
26-
req = client.get(url, timeout=5)
27-
if req.status_code == 200:
28-
return req.json().get("info", {}).get("version", None)
29-
else:
30-
return None
23+
req = await _client.get(url, timeout=5)
24+
if req.status_code == 200:
25+
return req.json().get("info", {}).get("version", None)
26+
else:
27+
return None
3128
except Exception as e:
3229
print(f"WARNING: Failed to check update from {url}", e)
3330
return None
@@ -56,16 +53,13 @@ async def check_update() -> Union[CheckStatus, str, None]:
5653
return CheckStatus.SKIPPED
5754
elif "FAILED" in (__version__.tag_name, __version__.version):
5855
return CheckStatus.FAILED
59-
6056
else:
61-
loop = asyncio.get_event_loop()
62-
pypi = loop.run_in_executor(_executor, _sync_get_from_pypi, PYPI_API)
63-
tsinghua_pypi = loop.run_in_executor(
64-
_executor, _sync_get_from_pypi, TSINGHUA_PYPI_API
57+
vers = await asyncio.gather(
58+
_get_from_pypi(PYPI_API),
59+
_get_from_pypi(TSINGHUA_PYPI_API),
60+
return_exceptions=True,
6561
)
6662

67-
vers = await asyncio.gather(pypi, tsinghua_pypi, return_exceptions=True)
68-
6963
check_number = len(vers)
7064
check_succeed_number = 0
7165

0 commit comments

Comments
 (0)