Переглянути джерело

异步推送消息和保存数据

locky 1 рік тому
батько
коміт
b20be9ef99
1 змінених файлів з 20 додано та 6 видалено
  1. 20 6
      Controller/DetectControllerV2.py

+ 20 - 6
Controller/DetectControllerV2.py

@@ -1,5 +1,6 @@
 import json
 import logging
+import threading
 
 from django.http import JsonResponse
 from django.views.generic.base import View
@@ -124,15 +125,16 @@ class NotificationV2View(View):
                       'dealings_type': dealings_type, 'detection': detection,
                       'app_push_config': uid_set_push_list[0]['uid_set__msg_notify']}
 
-            # 推送消息,生成推送数据列表
-            result = DevicePushService.save_msg_push(uid_set_push_list, **params)
-            # 保存推送数据
-            DevicePushService.save_sys_msg(is_sys_msg, result['local_date_time'],
-                                           result['sys_msg_list'], result['new_device_info_list'])
+            # 异步推送消息和保存数据
+            push_thread = threading.Thread(
+                target=push_and_save_data,
+                args=(uid_set_push_list, is_sys_msg),
+                kwargs=params)
+            push_thread.start()
 
             params['aws_s3_client'] = aws_s3_client
             params['uid_set_push_list'] = uid_set_push_list
-            params['code_dict'] = result
+
             result_dict = DevicePushService.get_push_url(**params)  # 获取S3对象上传链接
             TIME_LOGGER.info('推送响应,uid:{},n_time:{},事件类型:{},响应:{}'.format(
                 uid, n_time, event_type, json.dumps(result_dict)))
@@ -144,3 +146,15 @@ class NotificationV2View(View):
                 'errMsg': repr(e),
             }
             return JsonResponse(status=200, data=json.dumps(data), safe=False)
+
+
+def push_and_save_data(uid_set_push_list, is_sys_msg, **params):
+    # 推送消息,生成推送数据列表
+    result = DevicePushService.save_msg_push(uid_set_push_list, **params)
+    # 保存推送数据
+    DevicePushService.save_sys_msg(
+        is_sys_msg,
+        result['local_date_time'],
+        result['sys_msg_list'],
+        result['new_device_info_list'])
+