feat: cd BrokenPipeError时尝试再建立连接3
This commit is contained in:
parent
facee9342c
commit
60c484ee7f
|
@ -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)}"})
|
||||
|
|
Loading…
Reference in New Issue