Jelajahi Sumber

通过线程池推送

locky 1 tahun lalu
induk
melakukan
068ecd4c3e
1 mengubah file dengan 9 tambahan dan 10 penghapusan
  1. 9 10
      Service/CustomizedPushService.py

+ 9 - 10
Service/CustomizedPushService.py

@@ -4,9 +4,9 @@
 import logging
 import threading
 import time
+from concurrent.futures import ThreadPoolExecutor
 
 from Model.models import DeviceTypeModel, Device_Info, GatewayPush, CountryModel, SysMsgModel
-from Object.RedisObject import RedisObject
 from Service.CommonService import CommonService
 from Service.HuaweiPushService.HuaweiPushService import HuaweiPushObject
 from Service.PushService import PushObject
@@ -116,7 +116,7 @@ class CustomizedPushObject:
                     'gateway_push_list': gateway_push_list
                 }
                 pre_push_thread = threading.Thread(
-                    target=cls.pre_push,
+                    target=cls.thr_pool_push,
                     kwargs=pre_push_kwargs)
                 pre_push_thread.start()
                 CUSTOMIZED_PUSH_LOGGER.info('customized_push_id:{}推送完成'.format(kwargs['id']))
@@ -125,18 +125,17 @@ class CustomizedPushObject:
                                         'error_line:{},error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
     @classmethod
-    def pre_push(cls, **kwargs):
-        CUSTOMIZED_PUSH_LOGGER.info('预推送线程开始')
+    def thr_pool_push(cls, **kwargs):
+        CUSTOMIZED_PUSH_LOGGER.info('线程池推送开始')
         push_kwargs = kwargs['push_kwargs']
         gateway_push_list = kwargs['gateway_push_list']
+        push_kwargs_list = []
         for gateway_push in gateway_push_list:
-            # 异步推送消息
             push_kwargs['gateway_push'] = gateway_push
-            push_thread = threading.Thread(
-                target=cls.start_push,
-                kwargs=push_kwargs)
-            push_thread.start()
-        CUSTOMIZED_PUSH_LOGGER.info('预推送线程完成')
+            push_kwargs_list.append(push_kwargs)
+        with ThreadPoolExecutor() as executor:
+            executor.map(cls.start_push, push_kwargs_list)
+        CUSTOMIZED_PUSH_LOGGER.info('线程池推送完成')
 
     @classmethod
     def start_push(cls, **kwargs):