feat: cd 设置清空缓冲区2

This commit is contained in:
caoqianming 2025-06-23 17:49:02 +08:00
parent c5e4cc48a4
commit b0df18f01a
1 changed files with 20 additions and 15 deletions

View File

@ -7,6 +7,7 @@ from apps.utils.thread import MyThread
import struct
import uuid
import logging
import threading
myLogger = logging.getLogger('log')
@ -133,8 +134,9 @@ def get_tyy_data_2(*args, retry=1):
sc_all = {}
sc_lock = threading.Lock()
def get_tyy_data(*args):
def get_tyy_data(*args, retry=1):
host, port = args[0], int(args[1])
global sc_all
sc = None
@ -151,11 +153,13 @@ def get_tyy_data(*args):
else:
# 清空接收缓冲区
sc.settimeout(0.1) # 设置短暂超时
try:
while True:
sc.recv(1024)
except (socket.timeout, BlockingIOError):
pass
for _ in range(5):
try:
data = sc.recv(65536)
if not data:
break
except (socket.timeout, BlockingIOError):
break
sc.settimeout(5) # 恢复原超时设置
sc.sendall(b"R")
except BrokenPipeError:
@ -194,12 +198,13 @@ def get_tyy_data(*args):
sc_all.pop(f"{host}_{port}", None)
sc = None
raise ParseError(f"采集器连接超时-{str(e)}")
connect_and_send()
resp = sc.recv(1024)
res = handle_bytes(resp)
# myLogger.error(res)
if isinstance(res, str):
raise ParseError(f'采集器返回数据错误-{res}')
else:
return res
with sc_lock:
connect_and_send()
resp = sc.recv(1024)
res = handle_bytes(resp)
# myLogger.error(res)
if isinstance(res, str):
raise ParseError(f'采集器返回数据错误-{res}')
else:
return res