job_cover_method.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import time
  2. from concurrent.futures import ThreadPoolExecutor, wait
  3. from common.redis import get_pq_id
  4. from video_cover_method.cover_method import CoverMethod
  5. # 限制最大线程数为 10
  6. max_workers = 10
  7. def get_video_id():
  8. video_ids = set() # 使用集合去重
  9. for i in range(10):
  10. video_id = get_pq_id()
  11. if video_id:
  12. video_ids.add(video_id)
  13. else:
  14. return list( video_ids )
  15. return list( video_ids )
  16. def process_video_cover(video_id):
  17. try:
  18. CoverMethod.cover_method( int( video_id ) )
  19. except Exception as e:
  20. print( "处理任务时出现异常:", e)
  21. def video_cover_task_start():
  22. with ThreadPoolExecutor( max_workers=max_workers) as executor:
  23. while True:
  24. video_ids = get_video_id()
  25. if not video_ids:
  26. print("没有数据等待30秒")
  27. time.sleep(30)
  28. continue
  29. # 提交所有任务并等待完成
  30. futures = [executor.submit( process_video_cover, video_id ) for video_id in video_ids]
  31. wait( futures ) # 等待所有任务完成
  32. if __name__ == '__main__':
  33. video_cover_task_start()