run_video_publish_and_audit.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """
  2. @author: luojunhui
  3. """
  4. import time
  5. import threading
  6. from concurrent.futures import ThreadPoolExecutor, as_completed
  7. from coldStartTasks.publish import PublishVideosForAudit
  8. from applications.const import WeixinVideoCrawlerConst
  9. const = WeixinVideoCrawlerConst()
  10. pub = PublishVideosForAudit()
  11. def run_thread(task_type: str, wait_time: int, stop_event: threading.Event):
  12. """
  13. 运行线程
  14. :param task_type:
  15. :param wait_time:
  16. :param stop_event:
  17. :return:
  18. """
  19. while not stop_event.is_set():
  20. if task_type == const.PUBLISH_TASK:
  21. # 发布视频
  22. pub.publish_job()
  23. time.sleep(wait_time)
  24. elif task_type == const.CHECK_TASK:
  25. # 检查视频
  26. pub.check_job()
  27. time.sleep(wait_time)
  28. def main():
  29. """
  30. 主函数
  31. :return:
  32. """
  33. stop_event = threading.Event()
  34. # 启动两个线程,分别执行两个函数
  35. with ThreadPoolExecutor(max_workers=2) as executor:
  36. futures = [
  37. executor.submit(run_thread, const.PUBLISH_TASK, const.PUBLISH_TASK_SLEEP_TEME, stop_event),
  38. executor.submit(run_thread, const.CHECK_TASK, const.CHECK_TASK_SLEEP_TEME, stop_event)
  39. ]
  40. try:
  41. for future in as_completed(futures):
  42. future.result()
  43. except KeyboardInterrupt:
  44. print("Stopping all threads...")
  45. stop_event.set()
  46. if __name__ == '__main__':
  47. main()