def CheckLoadPicture(self):
if not self.maxPic or not self.bookId:
return
# 扩展预加载范围,包括前向和后向
forward_range = min(self.curIndex + config.PreLoading, self.maxPic)
backward_range = max(self.curIndex - config.PreLoading//2, 0) # 增加向后预加载
preLoadSet = set(range(backward_range, forward_range))
# 保留当前页及其邻近页面
currentKeys = set(self.pictureData.keys())
removeKeys = currentKeys - preLoadSet
# 批量移除不需要的任务
removeTaskIds = []
for key in removeKeys:
p = self.pictureData[key]
if p.waifu2xTaskId > 0:
removeTaskIds.append(p.waifu2xTaskId)
# 清理不需要的数据
for key in removeKeys:
self.pictureData.pop(key, None)
if removeTaskIds:
self.ClearWaitConvertIds(removeTaskIds)
# 优化下载和转换任务调度
download_tasks = []
convert_tasks = []
for i in sorted(preLoadSet):
if i >= self.maxPic or i < 0:
continue
p = self.pictureData.get(i)
# 添加下载任务
if not p:
download_tasks.append(i)
elif p.state in [p.DownloadReset, p.DownloadError]:
download_tasks.append(i)
elif p.state == p.Downloading:
continue
# 添加转换任务
if p and p.isWaifu2x and p.data and not p.cacheWaifu2xImage:
if p.waifuState in [p.WaifuStateCancle, p.WaifuWait]:
p.waifuState = p.WaifuStateStart
convert_tasks.append(i)
# 控制并发数量
max_download_concurrent = 3
max_convert_concurrent = 2
# 批量添加下载任务(优先处理临近当前页的)
near_tasks = [i for i in download_tasks if abs(i - self.curIndex) <= 2]
far_tasks = [i for i in download_tasks if abs(i - self.curIndex) > 2]
# 优先处理靠近当前页的下载任务
for i in (near_tasks + far_tasks)[:max_download_concurrent]:
self.AddDownload(i)
# 批量添加转换任务
for i in convert_tasks[:max_convert_concurrent]:
p = self.pictureData.get(i)
if p and p.waifu2xTaskId <= 0:
self.AddCovertData(i)
# 处理QImage转换任务
preQImage = list(range(self.curIndex - 1, self.curIndex + config.PreLook))
preRealQImage = list(range(self.curIndex - 1, self.curIndex + config.PreLook))
for i in preRealQImage:
if i < 0 or i >= self.maxPic:
continue
p = self.pictureData.get(i)
if not p or not p.data:
continue
assert isinstance(p, QtFileData)
# 优先处理当前页和临近页的图像转换
if i == self.curIndex or abs(i - self.curIndex) <= 1:
# 立即处理当前页和相邻页
if not (not p.waifuData or p.cacheWaifu2xImage or p.cacheWaifu2xImageTaskId > 0):
self.CheckToQImage(i, p, True)
if not (p.cacheImage or p.cacheImageTaskId > 0):
self.CheckToQImage(i, p, False)
else:
# 后续处理其他页面
IsConvert = (p.cacheWaifu2xImageTaskId > 0 or not not p.cacheWaifu2xImage)
if not (not p.waifuData or p.cacheWaifu2xImage or p.cacheWaifu2xImageTaskId > 0):
IsConvert = self.CheckToQImage(i, p, True)
if (not IsConvert or not p.isWaifu2x) and not (p.cacheImage or p.cacheImageTaskId > 0):
self.CheckToQImage(i, p, False)
# 清理不需要的QImage缓存
for i in set(self.pictureData.keys()) - set(preQImage):
p = self.pictureData.get(i)
if not p:
continue
assert isinstance(p, QtFileData)
if p.cacheImageTaskId > 0:
self.ClearQImageTaskById(p.cacheImageTaskId)
p.cacheImageTaskId = 0
if p.cacheWaifu2xImageTaskId > 0:
self.ClearQImageTaskById(p.cacheWaifu2xImageTaskId)
p.cacheWaifu2xImageTaskId = 0
p.cacheImage = None
p.cacheWaifu2xImage = None
功能描述(请清晰的、详细的描述你想要的功能)
附加信息(其他的与功能相关的附加信息)
效果演示(可以提供可借鉴的图片)
实测是顺畅的。