deal_cutout.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import time
  2. from concurrent.futures import as_completed, ThreadPoolExecutor, wait
  3. import threading
  4. from module.remove_bg_pixian import RemoveBgPiXian
  5. from PySide6.QtWidgets import *
  6. from PySide6.QtCore import *
  7. from module.other.module_online_data import GetOnlineData
  8. from module.deal_one_image import DealOneImage, DealOneImageBeforehand
  9. from module.other.log import MyLogger
  10. class DealCutout(QThread):
  11. signal_data = Signal(dict)
  12. def __init__(self, windows):
  13. super().__init__()
  14. self.windows = windows
  15. self.lock = threading.Lock()
  16. self.need_cutout_images = {}
  17. self.state = 2 # 1进行中 2停止
  18. self.get_online_data = GetOnlineData()
  19. self.is_upload_pic_num = 0
  20. self.is_deal_num = 0
  21. # 图片列表
  22. self.upload_pic_dict = {}
  23. self.logger = MyLogger().logger
  24. def run(self):
  25. # self.total_num = len([x for x in self.need_cutout_images if x["need_cutout"]]) # 总条数
  26. # self.pending_processing = int(self.total_num) # 待处理
  27. # self.processing_failed = 0 # 处理失败
  28. # self.processing_successfully = 0 # 处理成功
  29. # self.processing_error = 0 # 处理异常
  30. self.get_online_data.refresh_headers()
  31. executor = ThreadPoolExecutor(max_workers=4)
  32. executor_pic_upload = ThreadPoolExecutor(max_workers=2)
  33. tasks_1 = []
  34. tasks_2 = []
  35. self.state = 1
  36. self.is_upload_pic_num = 0
  37. self.is_deal_num = 0
  38. num = 0
  39. for image_data in self.need_cutout_images:
  40. if not image_data["need_cutout"]:
  41. continue
  42. num += 1
  43. task_1 = executor.submit(DealOneImage(image_data=image_data, lock=self.lock, windows=self, num=num).run)
  44. tasks_1.append(task_1)
  45. task_2 = executor_pic_upload.submit(
  46. DealOneImageBeforehand(image_data=image_data, lock=self.lock, windows=self, num=num).run)
  47. tasks_2.append(task_2)
  48. self.check_thread(tasks_1, tasks_2)
  49. def check_thread(self, *tasks_list):
  50. time.sleep(2)
  51. while 1:
  52. f = True
  53. for tasks in tasks_list:
  54. done, not_done = wait(tasks)
  55. if not_done:
  56. time.sleep(2)
  57. f = False
  58. continue
  59. for task in done:
  60. try:
  61. result = task.result()
  62. # print(result)
  63. except BaseException as e:
  64. self.logger.info("有线程出错:{}".format(e))
  65. if f:
  66. break
  67. self.signal_data.emit({"_type": "complete",
  68. "data": ""})