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

This commit is contained in:
caoqianming 2025-06-05 11:15:16 +08:00
parent 370c905781
commit facee9342c
1 changed files with 19 additions and 16 deletions

View File

@ -8,6 +8,17 @@ 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():
nonlocal sc
try:
sc = socket.socket()
sc.connect((host, int(port)))
except OSError as e:
cache.set(cd_thread_key, {"err_msg": f"采集器连接失败-{str(e)}"})
except ConnectionResetError:
cache.set(cd_thread_key, {"err_msg": "采集器重置了连接"})
except socket.timeout:
cache.set(cd_thread_key, {"err_msg": "采集器连接超时"})
while True:
cd_thread_val = cache.get(cd_thread_key, default=None)
if cd_thread_val is None:
@ -19,23 +30,15 @@ def get_tyy_data_t(host, port):
break
elif cd_thread_val == "get":
if sc is None:
connect()
try:
sc = socket.socket()
sc.connect((host, int(port)))
sc.sendall(b"R")
except BrokenPipeError:
try:
sc.close()
except Exception:
pass
sc = socket.socket()
sc.connect((host, int(port)))
except OSError as e:
cache.set(cd_thread_key, {"err_msg": f"采集器连接失败-{str(e)}"})
except ConnectionResetError:
cache.set(cd_thread_key, {"err_msg": "采集器重置了连接"})
except socket.timeout:
cache.set(cd_thread_key, {"err_msg": "采集器连接超时"})
sc.sendall(b"R")
connect()
resp = sc.recv(1024)
if not resp:
cache.set(cd_thread_key, {"err_msg": f"采集器返回空数据-{str(resp)}"})