|
1 | 1 | import asyncio |
| 2 | +from concurrent.futures import ThreadPoolExecutor |
2 | 3 | from enum import auto, Enum |
3 | 4 | from typing import Any, Optional, Union |
4 | 5 |
|
|
10 | 11 | PYPI_API = "https://pypi.org/pypi/MaaDebugger/json" |
11 | 12 | TSINGHUA_PYPI_API = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/json/maadebugger" |
12 | 13 |
|
| 14 | +_executor = ThreadPoolExecutor(max_workers=2) |
| 15 | + |
13 | 16 |
|
14 | 17 | class CheckStatus(Enum): |
15 | 18 | FAILED = auto() |
16 | 19 | SKIPPED = auto() |
17 | 20 |
|
18 | 21 |
|
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.""" |
20 | 24 | 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) |
23 | 27 | if req.status_code == 200: |
24 | 28 | return req.json().get("info", {}).get("version", None) |
25 | 29 | else: |
@@ -54,8 +58,11 @@ async def check_update() -> Union[CheckStatus, str, None]: |
54 | 58 | return CheckStatus.FAILED |
55 | 59 |
|
56 | 60 | 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 | + ) |
59 | 66 |
|
60 | 67 | vers = await asyncio.gather(pypi, tsinghua_pypi, return_exceptions=True) |
61 | 68 |
|
|
0 commit comments