Skip to content

Commit

Permalink
新增 自动加速 开关;防止开启自动加速时每次重启任务时都会新增两个线程;修复开启自动加速后,任务超过256线程时还会新增线程导致程序崩溃的问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoYouChR committed Nov 20, 2024
1 parent 29046ae commit 6cb71c4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Config(QConfig):
"Download", "DownloadFolder", QDir.currentPath(), FolderValidator())

maxBlockNum = RangeConfigItem("Download", "MaxBlockNum", 32, RangeValidator(1, 256))
autoSpeedUp = ConfigItem("Download", "AutoSpeedUp", True, BoolValidator())
proxyServer = ConfigItem("Download", "ProxyServer", "Auto", ProxyValidator())

# browser
Expand Down
28 changes: 18 additions & 10 deletions app/common/download_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ class DownloadTask(QThread):
taskFinished = Signal() # 内置信号的不好用
gotWrong = Signal(str) # 😭 我出问题了

def __init__(self, url, preTaskNum: int = 8, filePath=None, fileName=None, autoCreateTask = True, parent=None):
def __init__(self, url, preTaskNum: int = 8, filePath=None, fileName=None, autoSpeedUp=cfg.autoSpeedUp.value, parent=None):
super().__init__(parent)

self.process = 0
self.url = url
self.fileName = fileName
self.filePath = filePath
self.preBlockNum = preTaskNum
self.autoCreateTask = autoCreateTask
self.autoSpeedUp = autoSpeedUp
self.workers: list[DownloadWorker] = []
self.tasks: list[Task] = []

Expand Down Expand Up @@ -234,7 +234,7 @@ async def __handleWorker(self, worker: DownloadWorker):
async def __supervisor(self):
"""实时统计进度并写入历史记录文件"""

if self.autoCreateTask:
if self.autoSpeedUp:
# 初始化变量
for i in self.workers:
self.process += (i.process - i.startPos + 1) # 最初为计算每个线程的平均速度
Expand Down Expand Up @@ -263,25 +263,33 @@ async def __supervisor(self):

self.workerInfoChange.emit(info)

if self.autoCreateTask:
if self.autoSpeedUp:
speed = (self.process - LastProcess) / 1
LastProcess = self.process
speedPerConnect = formerSpeed / len(self.tasks)

if speedPerConnect > maxSpeedPerConnect:
maxSpeedPerConnect = speedPerConnect


if maxSpeedPerConnect <= 1:
await asyncio.sleep(1)
continue

if formerSpeed == 0:
formerSpeed = speed
await asyncio.sleep(1)
continue

#print(f'{self.taskNum}\t{(speed - formerSpeed) / newTaskNum}\t{maxSpeedPerConnect}\t{(speed - formerSpeed) / newTaskNum / maxSpeedPerConnect}')

if (speed - formerSpeed) / newTaskNum / maxSpeedPerConnect >= 0.9:
if (speed - formerSpeed) / newTaskNum / maxSpeedPerConnect >= 0.85:
# 新增加线程的效率 >= 0.9 时,新增线程
logger.debug(f'自动提速增加新线程 {(speed - formerSpeed) / newTaskNum / maxSpeedPerConnect}')
formerSpeed = speed
newTaskNum = 1

self.__reassignWorker() # 新增线程

LastProcess = self.process

if len(self.tasks) < 256:
self.__reassignWorker() # 新增线程


await asyncio.sleep(1)
Expand Down
8 changes: 8 additions & 0 deletions app/view/setting_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ def __init__(self, parent=None):
self.downloadGroup
)

self.autoSpeedUpCard = SwitchSettingCard(
FIF.SPEED_HIGH,
"自动提速",
"实时检测线程效率并自动添加线程以提高下载速度 (Algorithm by @Alpha_Qian)",
cfg.autoSpeedUp,
self.downloadGroup
)

self.downloadFolderCard = PushSettingCard(
"选择文件夹",
Expand Down Expand Up @@ -362,6 +369,7 @@ def __initLayout(self):
# add cards to group
self.downloadGroup.addSettingCard(self.blockNumCard)
self.downloadGroup.addSettingCard(self.maxReassignSizeCard)
self.downloadGroup.addSettingCard(self.autoSpeedUpCard)
self.downloadGroup.addSettingCard(self.downloadFolderCard)
self.downloadGroup.addSettingCard(self.proxyServerCard)

Expand Down

0 comments on commit 6cb71c4

Please sign in to comment.