Skip to content

The only downloader you need. 下载器的集大成者。

优化速度平滑计算 (#207)

AlphaQian384e970359b2
diff --git a/app/common/download_task.py b/app/common/download_task.py
index 4c7ab8f9..09ce167e 100644
--- a/app/common/download_task.py
+++ b/app/common/download_task.py
@@ -7,6 +7,7 @@ from asyncio import Task
from pathlib import Path
from threading import Thread
from typing import List
+from collections import deque
import httpx
from PySide6.QtCore import QThread, Signal
@@ -222,7 +223,7 @@ class DownloadTask(QThread):
self.downloadStrategy = None
# Speed tracking
- self.historySpeed = [0] * 10 # Rolling window of 10 seconds for speed calculation
+ self.historyProgress = deque([self.progress] * 10) # Rolling window of 10 seconds for speed calculation
# HTTP client setup
proxy = getProxy()
@@ -490,9 +491,7 @@ class DownloadTask(QThread):
workerInfo = self.__updateProgressAndHistory() #用不到的 workerInfo
# Calculate and emit current speed
- currentSpeed = self.progress - lastProgress
- lastProgress = self.progress
- avgSpeed = self.__updateSpeedHistory(currentSpeed) #用不到 avgSpeed
+ self.__updateSmoothedSpeed()
# Handle auto speed-up if enabled
if self.autoSpeedUp:
@@ -545,9 +544,7 @@ class DownloadTask(QThread):
self.workerInfoChanged.emit([])
# Calculate and emit current speed
- currentSpeed = self.progress - lastProgress
- lastProgress = self.progress
- avgSpeed = self.__updateSpeedHistory(currentSpeed)
+ self.__updateSmoothedSpeed()
# Wait before next update
await asyncio.sleep(1)
@@ -584,11 +581,11 @@ class DownloadTask(QThread):
return workerInfo
- def __updateSpeedHistory(self, currentSpeed):
+ def __updateSmoothedSpeed(self):
"""Update speed history and calculate average speed"""
- self.historySpeed.pop(0)
- self.historySpeed.append(currentSpeed)
- avgSpeed = sum(self.historySpeed) / 10
+ formerProgress = self.historyProgress.pop()
+ self.historyProgress.appendleft(self.progress)
+ avgSpeed = (self.progress - formerProgress) / 10
self.speedChanged.emit(avgSpeed)
return avgSpeed