fix: 心跳总次数不再随着进度滞后越加越大

This commit is contained in:
2026-11-17 11:41:57 +08:00
parent 11a3c5611a
commit 2cb6623ac4
2 changed files with 44 additions and 8 deletions
+20 -8
View File
@@ -213,14 +213,11 @@ class WorkerView:
def after_beat(self, progress: float, **kw: object) -> None:
self.sent += 1
planned = _count_total(self.sent, progress, self._origin)
if self.total <= 0:
self.total = planned
elif progress >= 100:
if progress >= 100:
self.total = self.sent
elif planned > self.total:
self.total = planned
if self.sent > self.total:
elif self.total <= 0:
self.total = _count_total(self.sent, progress, self._origin)
if self.total > 0 and self.sent > self.total:
self.total = self.sent
self.update(progress=progress, sent=self.sent, total=self.total, **kw)
@@ -608,8 +605,18 @@ async def _open_play(session: "StudySession", path: str) -> dict:
return {}
def _planned_beats(cap: int, position: int, step: int) -> int:
"""用课件时长和每拍申报的秒数算出固定总次数,不随进度回写变大。"""
if cap <= 0 or step <= 0:
return 0
remaining = max(0, int(cap) - max(0, int(position)))
if remaining <= 0:
return 1
return max(1, math.ceil(remaining / int(step)))
def _count_total(sent: int, progress: float, origin: float = 0.0) -> int:
"""按开课进度估算剩余心跳数。总数在 WorkerView 里冻结,避免每拍都变。"""
"""没有课件时长时,用第一次看得到的进度估算总次数,之后不再上调。"""
if sent <= 0:
return 0
if progress >= 100:
@@ -1173,6 +1180,11 @@ async def _study_video(session: StudySession, kid: str, pkg_id: str,
if step <= 0:
step = 30
view = 0
cap = int(getattr(session, "_video_cap", 0) or 0)
if worker is not None and worker.total <= 0 and cap > 0:
start_at = _whole((snapshot or {}).get("viewSchedule"), 0)
worker.total = _planned_beats(cap, start_at, step)
worker.update(total=worker.total, sent=worker.sent)
started = False
stall = _Stall()
pending = snapshot