import time from concurrent.futures import as_completed, ThreadPoolExecutor, wait import threading from module.remove_bg_pixian import RemoveBgPiXian from PySide6.QtWidgets import * from PySide6.QtCore import * from module.other.module_online_data import GetOnlineData from module.deal_one_image import DealOneImage, DealOneImageBeforehand from module.other.log import MyLogger class DealCutout(QThread): signal_data = Signal(dict) def __init__(self, windows): super().__init__() self.windows = windows self.lock = threading.Lock() self.need_cutout_images = {} self.state = 2 # 1进行中 2停止 self.get_online_data = GetOnlineData() self.is_upload_pic_num = 0 self.is_deal_num = 0 # 图片列表 self.upload_pic_dict = {} self.logger = MyLogger().logger def run(self): # self.total_num = len([x for x in self.need_cutout_images if x["need_cutout"]]) # 总条数 # self.pending_processing = int(self.total_num) # 待处理 # self.processing_failed = 0 # 处理失败 # self.processing_successfully = 0 # 处理成功 # self.processing_error = 0 # 处理异常 self.get_online_data.refresh_headers() executor = ThreadPoolExecutor(max_workers=4) executor_pic_upload = ThreadPoolExecutor(max_workers=2) tasks_1 = [] tasks_2 = [] self.state = 1 self.is_upload_pic_num = 0 self.is_deal_num = 0 num = 0 for image_data in self.need_cutout_images: if not image_data["need_cutout"]: continue num += 1 task_1 = executor.submit(DealOneImage(image_data=image_data, lock=self.lock, windows=self, num=num).run) tasks_1.append(task_1) task_2 = executor_pic_upload.submit( DealOneImageBeforehand(image_data=image_data, lock=self.lock, windows=self, num=num).run) tasks_2.append(task_2) self.check_thread(tasks_1, tasks_2) def check_thread(self, *tasks_list): time.sleep(2) while 1: f = True for tasks in tasks_list: done, not_done = wait(tasks) if not_done: time.sleep(2) f = False continue for task in done: try: result = task.result() # print(result) except BaseException as e: self.logger.info("有线程出错:{}".format(e)) if f: break self.signal_data.emit({"_type": "complete", "data": ""})