feat: cd BrokenPipeError时尝试再建立连接3

This commit is contained in:
caoqianming 2025-06-05 13:39:24 +08:00
parent facee9342c
commit 60c484ee7f
1 changed files with 22 additions and 14 deletions

View File

@ -8,14 +8,28 @@ 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:
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:
sc = None
if retry > 0:
connect_and_send(retry-1)
else:
cache.set(cd_thread_key, {"err_msg": f"采集器连接失败-{str(e)}"})
except ConnectionResetError:
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": "采集器连接超时"})
@ -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)}"})