feat: cd BrokenPipeError时尝试再建立连接2
This commit is contained in:
parent
370c905781
commit
facee9342c
|
@ -8,6 +8,17 @@ from apps.utils.thread import MyThread
|
||||||
def get_tyy_data_t(host, port):
|
def get_tyy_data_t(host, port):
|
||||||
cd_thread_key = f"cd_thread_{host}_{port}"
|
cd_thread_key = f"cd_thread_{host}_{port}"
|
||||||
sc = None
|
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:
|
while True:
|
||||||
cd_thread_val = cache.get(cd_thread_key, default=None)
|
cd_thread_val = cache.get(cd_thread_key, default=None)
|
||||||
if cd_thread_val is None:
|
if cd_thread_val is None:
|
||||||
|
@ -19,23 +30,15 @@ def get_tyy_data_t(host, port):
|
||||||
break
|
break
|
||||||
elif cd_thread_val == "get":
|
elif cd_thread_val == "get":
|
||||||
if sc is None:
|
if sc is None:
|
||||||
|
connect()
|
||||||
|
try:
|
||||||
|
sc.sendall(b"R")
|
||||||
|
except BrokenPipeError:
|
||||||
try:
|
try:
|
||||||
sc = socket.socket()
|
sc.close()
|
||||||
sc.connect((host, int(port)))
|
except Exception:
|
||||||
except BrokenPipeError:
|
pass
|
||||||
try:
|
connect()
|
||||||
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")
|
|
||||||
resp = sc.recv(1024)
|
resp = sc.recv(1024)
|
||||||
if not resp:
|
if not resp:
|
||||||
cache.set(cd_thread_key, {"err_msg": f"采集器返回空数据-{str(resp)}"})
|
cache.set(cd_thread_key, {"err_msg": f"采集器返回空数据-{str(resp)}"})
|
||||||
|
|
Loading…
Reference in New Issue