feat: 自动学习按账号分开并发,不再用全站名额拖慢其他账号
This commit is contained in:
@@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
架构:
|
架构:
|
||||||
- 一个后台线程运行独立的 asyncio 事件循环
|
- 一个后台线程运行独立的 asyncio 事件循环
|
||||||
- 每个用户一个协程,asyncio.gather 并行
|
- 每个用户一个协程,互相不等待
|
||||||
- 全局 Semaphore 控制总并发,避免压爆上游
|
|
||||||
- 每个用户一个 StudySession(httpx.AsyncClient),代理仅 HTTP/HTTPS/SOCKS4/SOCKS5
|
- 每个用户一个 StudySession(httpx.AsyncClient),代理仅 HTTP/HTTPS/SOCKS4/SOCKS5
|
||||||
- 每个用户可同时学 1 到 8 门课。每门课一个协程和一份 CourseSession,各自计时、发心跳、读进度
|
- 每个用户可同时学 1 到 8 门课。每门课一个协程和一份 CourseSession,各自计时、发心跳、读进度
|
||||||
|
- 同时发出的请求数等于该用户正在学的课程数,不与其他用户共用名额
|
||||||
- 心跳间隔用 asyncio.sleep,不占线程。间隔记在内存里,保存配置后正在学的课下一拍就按新值走
|
- 心跳间隔用 asyncio.sleep,不占线程。间隔记在内存里,保存配置后正在学的课下一拍就按新值走
|
||||||
- 本月学时:看完一门课后刷新,学习过程中每 1 分钟刷新;达到阈值即停止该账号
|
- 本月学时:看完一门课后刷新,学习过程中每 1 分钟刷新;达到阈值即停止该账号
|
||||||
"""
|
"""
|
||||||
@@ -50,7 +50,6 @@ from euniv.study_window import as_cn, next_fire_at, next_month_start
|
|||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
_MAX_CONCURRENT_USERS = 8
|
|
||||||
_HOURS_REFRESH_SECONDS = 60.0
|
_HOURS_REFRESH_SECONDS = 60.0
|
||||||
_KEEP_LIVE = frozenset({"已达标", "已停止", "会话失效", "学习失败", "已学完"})
|
_KEEP_LIVE = frozenset({"已达标", "已停止", "会话失效", "学习失败", "已学完"})
|
||||||
|
|
||||||
@@ -1016,14 +1015,10 @@ class StudySession:
|
|||||||
|
|
||||||
async def _http(self, method: str, url: str, **kw: object) -> httpx.Response:
|
async def _http(self, method: str, url: str, **kw: object) -> httpx.Response:
|
||||||
assert self._client is not None
|
assert self._client is not None
|
||||||
limit = getattr(self, "_request_limit", None)
|
|
||||||
|
|
||||||
async def once() -> httpx.Response:
|
async def once() -> httpx.Response:
|
||||||
assert self._client is not None
|
assert self._client is not None
|
||||||
if limit is None:
|
return await self._client.request(method, url, **kw)
|
||||||
return await self._client.request(method, url, **kw)
|
|
||||||
async with limit:
|
|
||||||
return await self._client.request(method, url, **kw)
|
|
||||||
|
|
||||||
used = self._proxy
|
used = self._proxy
|
||||||
try:
|
try:
|
||||||
@@ -1796,7 +1791,6 @@ class AutoStudyWorker:
|
|||||||
self._async_stop: asyncio.Event | None = None
|
self._async_stop: asyncio.Event | None = None
|
||||||
self._kick: asyncio.Event | None = None
|
self._kick: asyncio.Event | None = None
|
||||||
self._tasks: dict[str, asyncio.Task] = {}
|
self._tasks: dict[str, asyncio.Task] = {}
|
||||||
self._sem: asyncio.Semaphore | None = None
|
|
||||||
|
|
||||||
def start(self) -> None:
|
def start(self) -> None:
|
||||||
self._thread = threading.Thread(target=self._run_thread, name="euniv-autostudy", daemon=True)
|
self._thread = threading.Thread(target=self._run_thread, name="euniv-autostudy", daemon=True)
|
||||||
@@ -1827,7 +1821,6 @@ class AutoStudyWorker:
|
|||||||
async def _main_loop(self) -> None:
|
async def _main_loop(self) -> None:
|
||||||
assert self._async_stop is not None
|
assert self._async_stop is not None
|
||||||
assert self._kick is not None
|
assert self._kick is not None
|
||||||
self._sem = asyncio.Semaphore(_MAX_CONCURRENT_USERS)
|
|
||||||
while not self._async_stop.is_set():
|
while not self._async_stop.is_set():
|
||||||
self._kick.clear()
|
self._kick.clear()
|
||||||
try:
|
try:
|
||||||
@@ -1976,10 +1969,9 @@ class AutoStudyWorker:
|
|||||||
await asyncio.to_thread(_release_pool, emp_id, True)
|
await asyncio.to_thread(_release_pool, emp_id, True)
|
||||||
return
|
return
|
||||||
hours_source = await asyncio.to_thread(hours_source_of)
|
hours_source = await asyncio.to_thread(hours_source_of)
|
||||||
assert self._sem is not None
|
|
||||||
await self._study_user(
|
await self._study_user(
|
||||||
emp_id, target, pw, cred, proxy, workers, use_pool,
|
emp_id, target, pw, cred, proxy, workers, use_pool,
|
||||||
course_list, hours_source, self._sem,
|
course_list, hours_source,
|
||||||
)
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
log.warning("用户 %s 自动学习失败: %s", emp_id, exc)
|
log.warning("用户 %s 自动学习失败: %s", emp_id, exc)
|
||||||
@@ -2001,7 +1993,7 @@ class AutoStudyWorker:
|
|||||||
async def _study_user(self, employee_id: str, target: int,
|
async def _study_user(self, employee_id: str, target: int,
|
||||||
password: str, credential: dict, proxy: str | None,
|
password: str, credential: dict, proxy: str | None,
|
||||||
workers: int, use_pool: bool, course_list: list,
|
workers: int, use_pool: bool, course_list: list,
|
||||||
hours_source: str, sem: asyncio.Semaphore) -> None:
|
hours_source: str) -> None:
|
||||||
assert self._async_stop is not None
|
assert self._async_stop is not None
|
||||||
user_stop = asyncio.Event()
|
user_stop = asyncio.Event()
|
||||||
with _halt_lock:
|
with _halt_lock:
|
||||||
@@ -2045,7 +2037,6 @@ class AutoStudyWorker:
|
|||||||
async with StudySession(
|
async with StudySession(
|
||||||
token, cookies, proxy=proxy, employee_id=employee_id, use_pool=use_pool
|
token, cookies, proxy=proxy, employee_id=employee_id, use_pool=use_pool
|
||||||
) as session:
|
) as session:
|
||||||
session._request_limit = sem
|
|
||||||
if user_stop.is_set():
|
if user_stop.is_set():
|
||||||
_set_user_status(employee_id, state="已停止")
|
_set_user_status(employee_id, state="已停止")
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user