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 struct
import uuid import uuid
import logging import logging
import threading
myLogger = logging.getLogger('log') myLogger = logging.getLogger('log')
@ -133,8 +134,9 @@ def get_tyy_data_2(*args, retry=1):
sc_all = {} 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]) host, port = args[0], int(args[1])
global sc_all global sc_all
sc = None sc = None
@ -151,11 +153,13 @@ def get_tyy_data(*args):
else: else:
# 清空接收缓冲区 # 清空接收缓冲区
sc.settimeout(0.1) # 设置短暂超时 sc.settimeout(0.1) # 设置短暂超时
for _ in range(5):
try: try:
while True: data = sc.recv(65536)
sc.recv(1024) if not data:
break
except (socket.timeout, BlockingIOError): except (socket.timeout, BlockingIOError):
pass break
sc.settimeout(5) # 恢复原超时设置 sc.settimeout(5) # 恢复原超时设置
sc.sendall(b"R") sc.sendall(b"R")
except BrokenPipeError: except BrokenPipeError:
@ -195,6 +199,7 @@ def get_tyy_data(*args):
sc = None sc = None
raise ParseError(f"采集器连接超时-{str(e)}") raise ParseError(f"采集器连接超时-{str(e)}")
with sc_lock:
connect_and_send() connect_and_send()
resp = sc.recv(1024) resp = sc.recv(1024)
res = handle_bytes(resp) res = handle_bytes(resp)