fix: 切换或退出登录前先暂停任务,重新登录后继续失效任务

This commit is contained in:
2026-10-25 07:12:35 +08:00
parent cd1aeb1d0f
commit 81f7f16712
2 changed files with 65 additions and 9 deletions
+33
View File
@@ -7,6 +7,7 @@ import base64
import calendar
import json
import re
import time
from collections import deque
from collections.abc import AsyncIterator
from dataclasses import dataclass, field
@@ -126,6 +127,9 @@ class NqiClient:
client: httpx.AsyncClient = field(init=False)
def __post_init__(self) -> None:
self._inflight = 0
self._idle = asyncio.Event()
self._idle.set()
self.client = httpx.AsyncClient(
headers={
"User-Agent": USER_AGENT,
@@ -140,10 +144,39 @@ class NqiClient:
max_keepalive_connections=POOL_CONNECTIONS,
),
)
raw_send = self.client.send
async def send(request, *args, **kwargs):
self._inflight += 1
self._idle.clear()
try:
return await raw_send(request, *args, **kwargs)
finally:
self._inflight = max(0, self._inflight - 1)
if self._inflight == 0:
self._idle.set()
self.client.send = send
async def aclose(self) -> None:
await self.client.aclose()
async def wait_idle(self, timeout: float = 5.0) -> None:
"""等在途请求结束。到时仍未结束也返回,调用方再关闭客户端。"""
deadline = time.monotonic() + timeout
while True:
if self._inflight == 0:
await asyncio.sleep(0)
if self._inflight == 0:
return
remaining = deadline - time.monotonic()
if remaining <= 0:
return
try:
await asyncio.wait_for(self._idle.wait(), min(0.2, remaining))
except TimeoutError:
continue
def export_cookies(self) -> list[dict[str, str]]:
exported: list[dict[str, str]] = []
for cookie in self.client.cookies.jar: