The only downloader you need. 下载器的集大成者。
TypeScript asyncasynciobittorentbtcross-platformdownload-managerdownloaderfluent-designftphttp3magnetpyqtpyside6pythonqtquicreactsoftwarestreaming
优化性能
XiaoYouChR053efaebfb13
diff --git a/app/common/download_task.py b/app/common/download_task.pyindex 34c9b136..fddc6978 100644--- a/app/common/download_task.py+++ b/app/common/download_task.py@@ -179,34 +179,22 @@ class DownloadTask(QThread):while not finished:try:download_headers = self.headers.copy()- if self.ableToParallelDownload:- download_headers["range"] = f"bytes={worker.process}-{worker.endPos}" # 添加范围-- async with worker.client.stream(url=self.url, headers=download_headers, timeout=30,- method="GET") as res:- async for chunk in res.aiter_bytes(chunk_size=65536): # aiter_content 的单位是字节, 即每64K写一次文件- if worker.endPos <= worker.process:- break- if chunk:- async with self.aioLock:- await self.file.seek(worker.process)- await self.file.write(chunk)- worker.process += 65536-- if worker.process >= worker.endPos and self.ableToParallelDownload:- worker.process = worker.endPos- else:- async with worker.client.stream(url=self.url, headers=download_headers, timeout=30,- method="GET") as res:- async for chunk in res.aiter_bytes(chunk_size=65536): # aiter_content 的单位是字节, 即每64K写一次文件- if chunk:- async with self.aioLock:- await self.file.seek(worker.process)- await self.file.write(chunk)- worker.process += len(chunk)+ download_headers["range"] = f"bytes={worker.process}-{worker.endPos}" # 添加范围++ async with worker.client.stream(url=self.url, headers=download_headers, timeout=30,+ method="GET") as res:+ async for chunk in res.aiter_bytes(chunk_size=65536): # aiter_content 的单位是字节, 即每64K写一次文件+ if worker.endPos <= worker.process:+ break+ if chunk:+ async with self.aioLock:+ await self.file.seek(worker.process)+ await self.file.write(chunk)+ worker.process += 65536- self.ableToParallelDownload = True # 事实上用来表示任务已经完成+ if worker.process >= worker.endPos:+ worker.process = worker.endPosfinished = True@@ -220,8 +208,37 @@ class DownloadTask(QThread):worker.process = worker.endPos- if self.ableToParallelDownload:- self.__reassignWorker()+ self.__reassignWorker()++ async def __handleWorkerWhenUnableToParallelDownload(self, worker: DownloadWorker):+ if worker.process < worker.endPos: # 因为可能会创建空线程+ finished = False+ while not finished:+ try:+ download_headers = self.headers.copy()+ async with worker.client.stream(url=self.url, headers=download_headers, timeout=30,+ method="GET") as res:+ async for chunk in res.aiter_bytes(chunk_size=65536): # aiter_content 的单位是字节, 即每64K写一次文件++ if chunk:+ async with self.aioLock:+ await self.file.seek(worker.process)+ await self.file.write(chunk)+ worker.process += len(chunk)++ self.ableToParallelDownload = True # 事实上用来表示任务已经完成++ finished = True++ except httpx.HTTPError as e:+ logger.info(+ f"Task: {self.fileName}, Thread {worker} is reconnecting to the server, Error: {repr(e)}")++ self.gotWrong.emit(repr(e))++ await asyncio.sleep(5)++ worker.process = worker.endPosasync def __supervisor(self):"""实时统计进度并写入历史记录文件"""@@ -325,16 +342,19 @@ class DownloadTask(QThread):# 打开下载文件self.file = await aiofiles.open(f"{self.filePath}/{self.fileName}", "rb+")- # 启动 Worker- for i in self.workers:- logger.debug(f"Task {self.fileName}, starting the thread {i}...")+ if self.ableToParallelDownload:+ for i in self.workers: # 启动 Worker+ logger.debug(f"Task {self.fileName}, starting the thread {i}...")- _ = asyncio.create_task(self.__handleWorker(i))+ _ = asyncio.create_task(self.__handleWorker(i))- self.tasks.append(_)+ self.tasks.append(_)- if self.ableToParallelDownload:self.ghdFile = await aiofiles.open(f"{self.filePath}/{self.fileName}.ghd", "wb")+ else:+ logger.debug(f"Task {self.fileName}, starting single thread...")+ _ = asyncio.create_task(self.__handleWorkerWhenUnableToParallelDownload(self.workers[0]))+ self.tasks.append(_)self.supervisorTask = asyncio.create_task(self.__supervisor())diff --git a/app/components/Ui_AddTaskOptionDialog.py b/app/components/Ui_AddTaskOptionDialog.pyindex e187ec2a..ae3baf14 100644--- a/app/components/Ui_AddTaskOptionDialog.py+++ b/app/components/Ui_AddTaskOptionDialog.py@@ -8,7 +8,7 @@## WARNING! All changes made in this file will be lost when recompiling UI file!################################################################################-from PySide6.QtCore import (QCoreApplication, QMetaObject, QSize, Qt)+from PySide6.QtCore import (QCoreApplication, QSize, Qt)from PySide6.QtWidgets import (QHBoxLayout, QSizePolicy, QTableWidgetItem, QVBoxLayout, QApplication, QHeaderView)from qfluentwidgets import FluentIcon as FIFfrom qfluentwidgets import (PushButton, SubtitleLabel,@@ -123,7 +123,6 @@ class Ui_AddTaskOptionDialog(object):self.retranslateUi(AddTaskOptionDialog)- QMetaObject.connectSlotsByName(AddTaskOptionDialog)# setupUidef retranslateUi(self, AddTaskOptionDialog):diff --git a/app/components/Ui_TaskCard.py b/app/components/Ui_TaskCard.pyindex 3d8453f6..c2350ddc 100644--- a/app/components/Ui_TaskCard.py+++ b/app/components/Ui_TaskCard.py@@ -8,7 +8,7 @@## WARNING! All changes made in this file will be lost when recompiling UI file!################################################################################-from PySide6.QtCore import (QCoreApplication, QMetaObject, QSize, Qt)+from PySide6.QtCore import (QCoreApplication, QSize, Qt)from PySide6.QtWidgets import (QHBoxLayout, QSizePolicy, QVBoxLayout)from qfluentwidgets import FluentIcon as FIFfrom qfluentwidgets import (PixmapLabel, TitleLabel, ToolButton, PrimaryToolButton)@@ -139,7 +139,6 @@ class Ui_TaskCard(object):self.retranslateUi(TaskCard)- QMetaObject.connectSlotsByName(TaskCard)# setupUidiff --git a/app/components/Ui_UpdateDialog.py b/app/components/Ui_UpdateDialog.pyindex 9f6c5b76..af8986b4 100644--- a/app/components/Ui_UpdateDialog.py+++ b/app/components/Ui_UpdateDialog.py@@ -8,7 +8,7 @@## WARNING! All changes made in this file will be lost when recompiling UI file!################################################################################-from PySide6.QtCore import (QCoreApplication, QMetaObject)+from PySide6.QtCore import (QCoreApplication)from PySide6.QtWidgets import (QHBoxLayout,QSizePolicy, QVBoxLayout, QAbstractItemView, QHeaderView)@@ -102,8 +102,6 @@ class Ui_UpdateDialog(object):self.retranslateUi(UpdateDialog)-- QMetaObject.connectSlotsByName(UpdateDialog)# setupUidef retranslateUi(self, UpdateDialog):diff --git a/plugins/jy_os_page.py b/plugins/jy_os_page.pyindex 60734464..e5a8d3ab 100644--- a/plugins/jy_os_page.py+++ b/plugins/jy_os_page.py@@ -4,7 +4,7 @@ import osfrom pathlib import Pathimport httpx-from PySide6.QtCore import Qt, QThread, Signal, QDir, QUrl, QSize, QMetaObject, QCoreApplication+from PySide6.QtCore import Qt, QThread, Signal, QDir, QUrl, QSize, QCoreApplicationfrom PySide6.QtGui import QPixmap, QColor, QDesktopServicesfrom PySide6.QtWidgets import QWidget, QFrame, QVBoxLayout, QSizePolicy, QHBoxLayout, QFileDialogfrom loguru import logger@@ -319,7 +319,6 @@ class Ui_SystemInfoCard(object):self.retranslateUi(SystemInfoCard)- QMetaObject.connectSlotsByName(SystemInfoCard)# setupUi