Browse Source

bug fix, 线程池数据库连接冲突问题

luojunhui 4 months ago
parent
commit
99f6ffcedd
2 changed files with 15 additions and 44 deletions
  1. 0 6
      applications/const.py
  2. 15 38
      run_video_publish_and_audit.py

+ 0 - 6
applications/const.py

@@ -124,12 +124,6 @@ class WeixinVideoCrawlerConst:
     # 默认账号
     DEFAULT_ACCOUNT_UID = 76862180
 
-    # 任务执行
-    PUBLISH_TASK = "PUBLISH_TASK"
-    PUBLISH_TASK_SLEEP_TEME = 60 * 24 * 60
-    CHECK_TASK = "CHECK_TASK"
-    CHECK_TASK_SLEEP_TEME = 60
-
     # 每天发送的审核视频数量
     MAX_VIDEO_NUM = 500
 

+ 15 - 38
run_video_publish_and_audit.py

@@ -1,56 +1,33 @@
 """
 @author: luojunhui
 """
-import time
-import threading
-from concurrent.futures import ThreadPoolExecutor, as_completed
+from argparse import ArgumentParser
 
 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()
+    parser = ArgumentParser()
+    parser.add_argument("--run_task", type=str, help="run task, input publish or check")
+    args = parser.parse_args()
 
-    # 启动两个线程,分别执行两个函数
-    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 args.run_task:
+        task = args.run_task
+        if task == "publish":
+            pub.publish_job()
+        elif task == "check":
+            pub.check_job()
+        else:
+            print("run_task input ERROR,please input publish or check")
+    else:
+        pub.publish_job()
+        pub.check_job()
 
 
 if __name__ == '__main__':