From f324426b82216d30ffb6f3e57e82bb37efc2b985 Mon Sep 17 00:00:00 2001 From: Administrator <admin@example.com> Date: 星期五, 15 九月 2023 14:47:18 +0800 Subject: [PATCH] 订阅优化 --- huaxin_client/l2_client.py | 29 +++++++++++++++++++++++++---- 1 files changed, 25 insertions(+), 4 deletions(-) diff --git a/huaxin_client/l2_client.py b/huaxin_client/l2_client.py index 5046e87..f8d258c 100644 --- a/huaxin_client/l2_client.py +++ b/huaxin_client/l2_client.py @@ -146,6 +146,25 @@ self.__process_codes_data(codes_data) except Exception as e: logger_l2_codes_subscript.exception(e) + finally: + # 淇濆瓨涓�浠芥渶鏂扮殑鏁版嵁 + self.__set_latest_datas(codes_data) + + @classmethod + def __set_latest_datas(cls, codes_data): + data_str = json.dumps([tool.get_now_date_str(), codes_data]) + with open(constant.L2_CODES_INFO_PATH, mode='w') as f: + f.write(data_str) + + @classmethod + def __get_latest_datas(cls): + if os.path.exists(constant.L2_CODES_INFO_PATH): + with open(constant.L2_CODES_INFO_PATH, mode='r') as f: + str_ = f.readline() + data_json = json.loads(str_) + if data_json[0] == tool.get_now_date_str(): + return data_json[1] + return [] def set_code_special_watch_volume(self, code, volume): # 鏈夋晥鏈熶负2s @@ -168,9 +187,10 @@ if pRspInfo['ErrorID'] == 0: print("----L2琛屾儏鐧诲綍鎴愬姛----") self.is_login = True - # t1 = threading.Thread(target=lambda: self.__set_codes_data(), daemon=True) - # # 鍚庡彴杩愯 - # t1.start() + # 鍒濆璁剧疆鍊� + t1 = threading.Thread(target=lambda: self.__process_codes_data(self.__get_latest_datas()), daemon=True) + # 鍚庡彴杩愯 + t1.start() def OnRspSubMarketData(self, pSpecificSecurity, pRspInfo, nRequestID, bIsLast): print("OnRspSubMarketData") @@ -186,7 +206,8 @@ # try: print("璁㈤槄缁撴灉锛�", pSpecificSecurity["ExchangeID"], pSpecificSecurity["SecurityID"], pRspInfo["ErrorID"], pRspInfo["ErrorMsg"]) - async_log_util.info(logger_local_huaxin_l2_subscript, f"璁㈤槄缁撴灉锛歿pSpecificSecurity['SecurityID']} {pRspInfo['ErrorID']} {pRspInfo['ErrorMsg']}") + async_log_util.info(logger_local_huaxin_l2_subscript, + f"璁㈤槄缁撴灉锛歿pSpecificSecurity['SecurityID']} {pRspInfo['ErrorID']} {pRspInfo['ErrorMsg']}") if pRspInfo["ErrorID"] == 0: print("璁㈤槄鎴愬姛") self.subscripted_codes.add(pSpecificSecurity['SecurityID']) -- Gitblit v1.8.0