Skip to content

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

非常安全地暂停任务

XiaoYouChRa71530229b69
diff --git a/app/common/download_task.py b/app/common/download_task.py
index b01113f9..d6127215 100644
--- a/app/common/download_task.py
+++ b/app/common/download_task.py
@@ -1,6 +1,7 @@
import asyncio
import re
import struct
+import time
from asyncio import Task
from pathlib import Path
from threading import Thread
@@ -13,7 +14,7 @@ from app.common.config import cfg
from app.common.methods import getProxy, getReadableSize, getLinkInfo
Headers = {
- "accept-encoding": "gzip, deflate, br",
+ "accept-encoding": "deflate, br",
"accept-language": "zh-CN,zh;q=0.9",
"cookie": "down_ip=1",
"sec-fetch-dest": "document",
@@ -288,10 +289,13 @@ class DownloadTask(QThread):
self.tasks.append(_)
self.ghdFile = open(f"{self.filePath}/{self.fileName}.ghd", "wb")
- supervisorTask = asyncio.create_task(self.__supervisor())
+ self.supervisorTask = asyncio.create_task(self.__supervisor())
# 仅仅需要等待 supervisorTask
- await supervisorTask
+ try:
+ await self.supervisorTask # supervisorTask 被 cancel 后,会抛出 CancelledError, 所以之后的代码不会执行
+ except asyncio.CancelledError:
+ await self.client.aclose()
# 关闭
await self.client.aclose()
@@ -299,20 +303,35 @@ class DownloadTask(QThread):
self.file.close()
self.ghdFile.close()
- # 删除历史记录文件
- try:
- Path(f"{self.filePath}/{self.fileName}.ghd").unlink()
+ print("File closed.")
- except Exception as e:
- logger.error(f"Failed to delete the history file, please delete it manually. Err: {e}")
+ if self.process == self.fileSize:
+ # 删除历史记录文件
+ try:
+ Path(f"{self.filePath}/{self.fileName}.ghd").unlink()
- logger.info(f"Task {self.fileName} finished!")
+ except Exception as e:
+ logger.error(f"Failed to delete the history file, please delete it manually. Err: {e}")
- self.taskFinished.emit()
+ logger.info(f"Task {self.fileName} finished!")
+
+ self.taskFinished.emit()
except Exception as e:
self.gotWrong.emit(repr(e))
+ def stop(self):
+ for task in self.tasks:
+ task.cancel()
+
+ # 关闭
+ self.supervisorTask.cancel()
+ self.file.close()
+ self.ghdFile.close()
+
+ while not all(task.done() for task in self.tasks): # 等待所有任务完成
+ time.sleep(0.01)
+
# @retry(3, 0.1)
def run(self):
self.__tempThread.join()
diff --git a/app/components/custom_tray.py b/app/components/custom_tray.py
index cc600820..bda97dba 100644
--- a/app/components/custom_tray.py
+++ b/app/components/custom_tray.py
@@ -1,12 +1,9 @@
-import time
-
from PySide6.QtCore import QRect
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import QSystemTrayIcon, QApplication
from qfluentwidgets import Action
from qfluentwidgets import FluentIcon as FIF
from qfluentwidgets.components.material import AcrylicMenu
-from shiboken6.Shiboken import delete
class FixedAcrylicSystemTrayMenu(AcrylicMenu):
@@ -17,6 +14,7 @@ class FixedAcrylicSystemTrayMenu(AcrylicMenu):
self.adjustPosition()
self.view.acrylicBrush.grabImage(QRect(self.pos() + self.view.pos(), self.view.size()))
+
class CustomSystemTrayIcon(QSystemTrayIcon):
def __init__(self, parent=None):
@@ -26,7 +24,7 @@ class CustomSystemTrayIcon(QSystemTrayIcon):
self.menu = FixedAcrylicSystemTrayMenu(parent=parent)
- self.showAction = Action(QIcon(":/image/logo.png") ,'仪表盘', self.menu)
+ self.showAction = Action(QIcon(":/image/logo.png"), '仪表盘', self.menu)
self.showAction.triggered.connect(self.__onShowActionTriggered)
self.menu.addAction(self.showAction)
@@ -59,21 +57,15 @@ class CustomSystemTrayIcon(QSystemTrayIcon):
def __onAllPauseActionTriggered(self):
self.parent().taskInterface.allPauseTasks()
-
+
def __onQuitActionTriggered(self):
self.parent().themeChangedListener.terminate()
for i in self.parent().taskInterface.cards: # 是为了不写入历史记录安全的退出
if i.status == 'working':
- for j in i.task.tasks:
- j.cancel()
-
- while not all(j.done() for j in i.task.tasks):
- time.sleep(0.05)
+ i.task.stop()
- i.task.file.close()
- i.task.ghdFile.close()
- i.task.terminate()
+ # self.task.terminate()
i.task.wait()
i.task.deleteLater()
@@ -81,4 +73,4 @@ class CustomSystemTrayIcon(QSystemTrayIcon):
def onTrayIconClick(self, reason):
if reason == QSystemTrayIcon.Trigger:
- self.__onShowActionTriggered()
\ No newline at end of file
+ self.__onShowActionTriggered()
diff --git a/app/components/task_card.py b/app/components/task_card.py
index aac9ac89..b487cea0 100644
--- a/app/components/task_card.py
+++ b/app/components/task_card.py
@@ -1,6 +1,5 @@
import hashlib
import re
-import time
from pathlib import Path
from PySide6.QtCore import QThread, Signal, QFileInfo
@@ -9,7 +8,6 @@ from PySide6.QtWidgets import QFileIconProvider
from loguru import logger
from qfluentwidgets import CardWidget
from qfluentwidgets import FluentIcon as FIF
-from shiboken6.Shiboken import delete
from .Ui_TaskCard import Ui_TaskCard
from .del_dialog import DelDialog
@@ -146,15 +144,9 @@ class TaskCard(CardWidget, Ui_TaskCard):
self.pauseButton.setIcon(FIF.PLAY)
try:
- for i in self.task.tasks:
- i.cancel()
+ self.task.stop()
- while not all(i.done() for i in self.task.tasks):
- time.sleep(0.05)
-
- self.task.file.close()
- self.task.ghdFile.close()
- self.task.terminate()
+ # self.task.terminate()
self.task.wait()
self.task.deleteLater()
@@ -170,6 +162,9 @@ class TaskCard(CardWidget, Ui_TaskCard):
with open("{}/Ghost Downloader 记录文件".format(cfg.appPath), "w", encoding="utf-8") as f:
f.write(_)
+ except Exception as e:
+ logger.warning(f"Task:{self.fileName}, 暂停时遇到错误: {repr(e)}")
+
finally:
self.__showInfo("任务已经暂停")
self.status = "paused"