Browse Source

多线程启动historyTask

luojunhui 5 months ago
parent
commit
edb4af64bf
1 changed files with 11 additions and 7 deletions
  1. 11 7
      historyTask.py

+ 11 - 7
historyTask.py

@@ -6,6 +6,7 @@ import asyncio
 import datetime
 import datetime
 import traceback
 import traceback
 
 
+import threading
 from concurrent.futures import ThreadPoolExecutor, as_completed
 from concurrent.futures import ThreadPoolExecutor, as_completed
 
 
 from tasks.history_task import historyContentIdTask
 from tasks.history_task import historyContentIdTask
@@ -64,11 +65,11 @@ async def main(publish_flag):
     await history_content_id_task.deal(publish_flag)
     await history_content_id_task.deal(publish_flag)
 
 
 
 
-def run_thread(publish_flag, sleep_seconds):
+def run_thread(publish_flag, sleep_seconds, stop_flag):
     """
     """
     执行进程
     执行进程
     """
     """
-    while True:
+    while not stop_flag.is_set():
         loop = asyncio.new_event_loop()
         loop = asyncio.new_event_loop()
         asyncio.set_event_loop(loop)
         asyncio.set_event_loop(loop)
         loop.run_until_complete(main(publish_flag=publish_flag))
         loop.run_until_complete(main(publish_flag=publish_flag))
@@ -77,23 +78,26 @@ def run_thread(publish_flag, sleep_seconds):
         logging(
         logging(
             code="history0004",
             code="history0004",
             info="History task finished",
             info="History task finished",
-            alg="run_thread2"
+            alg="run_thread"
         )
         )
-        print("{}    请求执行完成, 等待{}s".format(now_str, sleep_seconds))
+        print("Task{}--{}    请求执行完成, 等待{}s".format(publish_flag, now_str, sleep_seconds))
         time.sleep(sleep_seconds)
         time.sleep(sleep_seconds)
 
 
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
     const = HistoryContentIdTaskConst()
     const = HistoryContentIdTaskConst()
 
 
+    stop_event = threading.Event()
+    # 启动两个线程,分别执行两个函数
     with ThreadPoolExecutor(max_workers=2) as executor:
     with ThreadPoolExecutor(max_workers=2) as executor:
         futures = [
         futures = [
-            executor.submit(run_thread, const.NEED_PUBLISH, const.NEED_PUBLISH_WAIT_TIME),
+            executor.submit(run_thread, const.NEED_PUBLISH, const.NEED_PUBLISH_WAIT_TIME, stop_event),
-            executor.submit(run_thread, const.DO_NOT_NEED_PUBLISH, const.DO_NOT_NEED_PUBLISH_WAIT_TIME)
+            executor.submit(run_thread, const.DO_NOT_NEED_PUBLISH, const.DO_NOT_NEED_PUBLISH_WAIT_TIME, stop_event)
         ]
         ]
 
 
         try:
         try:
             for future in as_completed(futures):
             for future in as_completed(futures):
                 future.result()
                 future.result()
         except KeyboardInterrupt:
         except KeyboardInterrupt:
-            print("Stopping all threads...")
+            print("Stopping all threads...")
+            stop_event.set()