123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- """
- @author: luojunhui
- """
- import time
- import threading
- from concurrent.futures import ThreadPoolExecutor, as_completed
- from coldStartTasks.publish import PublishVideosForAudit
- from applications.const import WeixinVideoCrawlerConst
- const = WeixinVideoCrawlerConst()
- pub = PublishVideosForAudit()
- def run_thread(task_type: str, wait_time: int, stop_event: threading.Event):
- """
- 运行线程
- :param task_type:
- :param wait_time:
- :param stop_event:
- :return:
- """
- while not stop_event.is_set():
- if task_type == const.PUBLISH_TASK:
- # 发布视频
- pub.publish_job()
- time.sleep(wait_time)
- elif task_type == const.CHECK_TASK:
- # 检查视频
- pub.check_job()
- time.sleep(wait_time)
- def main():
- """
- 主函数
- :return:
- """
- stop_event = threading.Event()
- # 启动两个线程,分别执行两个函数
- with ThreadPoolExecutor(max_workers=2) as executor:
- futures = [
- executor.submit(run_thread, const.PUBLISH_TASK, const.PUBLISH_TASK_SLEEP_TEME, stop_event),
- executor.submit(run_thread, const.CHECK_TASK, const.CHECK_TASK_SLEEP_TEME, stop_event)
- ]
- try:
- for future in as_completed(futures):
- future.result()
- except KeyboardInterrupt:
- print("Stopping all threads...")
- stop_event.set()
- if __name__ == '__main__':
- main()
|