Skip to content

Commit 01856c2

Browse files
committed
fix: 将检查更新移至后台线程池避免阻塞
1 parent 765e7a9 commit 01856c2

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

  • src/MaaDebugger/utils/update_checker

src/MaaDebugger/utils/update_checker/check.py

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

@@ -10,16 +11,19 @@
1011
PYPI_API = "https://pypi.org/pypi/MaaDebugger/json"
1112
TSINGHUA_PYPI_API = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/json/maadebugger"
1213

14+
_executor = ThreadPoolExecutor(max_workers=2)
15+
1316

1417
class CheckStatus(Enum):
1518
FAILED = auto()
1619
SKIPPED = auto()
1720

1821

19-
async def get_from_pypi(url: str) -> Optional[str]: # -> '1.8.0b1'
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."""
2024
try:
21-
async with httpx.AsyncClient() as client:
22-
req = await client.get(url, timeout=5)
25+
with httpx.Client() as client:
26+
req = client.get(url, timeout=5)
2327
if req.status_code == 200:
2428
return req.json().get("info", {}).get("version", None)
2529
else:
@@ -54,8 +58,11 @@ async def check_update() -> Union[CheckStatus, str, None]:
5458
return CheckStatus.FAILED
5559

5660
else:
57-
pypi = get_from_pypi(PYPI_API)
58-
tsinghua_pypi = get_from_pypi(TSINGHUA_PYPI_API)
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
65+
)
5966

6067
vers = await asyncio.gather(pypi, tsinghua_pypi, return_exceptions=True)
6168

0 commit comments

Comments
 (0)