From 60c484ee7fcff61821b98d1135e63d49182bc0d8 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Thu, 5 Jun 2025 13:39:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20cd=20BrokenPipeError=E6=97=B6=E5=B0=9D?= =?UTF-8?q?=E8=AF=95=E5=86=8D=E5=BB=BA=E7=AB=8B=E8=BF=9E=E6=8E=A53?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/em/cd.py | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/apps/em/cd.py b/apps/em/cd.py index 7c2a6050..f6b97ca4 100644 --- a/apps/em/cd.py +++ b/apps/em/cd.py @@ -8,15 +8,29 @@ from apps.utils.thread import MyThread def get_tyy_data_t(host, port): cd_thread_key = f"cd_thread_{host}_{port}" sc = None - def connect(): + def connect_and_send(retry=1): nonlocal sc try: - sc = socket.socket() - sc.connect((host, int(port))) + if sc is None: + sc = socket.socket() + sc.connect((host, int(port))) + sc.sendall(b"R") + except BrokenPipeError: + sc = None + if retry > 0: + connect_and_send(retry-1) except OSError as e: - cache.set(cd_thread_key, {"err_msg": f"采集器连接失败-{str(e)}"}) + sc = None + if retry > 0: + connect_and_send(retry-1) + else: + cache.set(cd_thread_key, {"err_msg": f"采集器连接失败-{str(e)}"}) except ConnectionResetError: - cache.set(cd_thread_key, {"err_msg": "采集器重置了连接"}) + sc = None + if retry > 0: + connect_and_send(retry-1) + else: + cache.set(cd_thread_key, {"err_msg": "采集器重置了连接"}) except socket.timeout: cache.set(cd_thread_key, {"err_msg": "采集器连接超时"}) while True: @@ -29,16 +43,10 @@ def get_tyy_data_t(host, port): pass break elif cd_thread_val == "get": + connect_and_send() if sc is None: - connect() - try: - sc.sendall(b"R") - except BrokenPipeError: - try: - sc.close() - except Exception: - pass - connect() + cache.set(cd_thread_key, {"err_msg": "采集器连接失败"}) + continue resp = sc.recv(1024) if not resp: cache.set(cd_thread_key, {"err_msg": f"采集器返回空数据-{str(resp)}"})