feat: cd 设置清空缓冲区2
This commit is contained in:
parent
c5e4cc48a4
commit
b0df18f01a
|
@ -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) # 设置短暂超时
|
||||||
try:
|
for _ in range(5):
|
||||||
while True:
|
try:
|
||||||
sc.recv(1024)
|
data = sc.recv(65536)
|
||||||
except (socket.timeout, BlockingIOError):
|
if not data:
|
||||||
pass
|
break
|
||||||
|
except (socket.timeout, BlockingIOError):
|
||||||
|
break
|
||||||
sc.settimeout(5) # 恢复原超时设置
|
sc.settimeout(5) # 恢复原超时设置
|
||||||
sc.sendall(b"R")
|
sc.sendall(b"R")
|
||||||
except BrokenPipeError:
|
except BrokenPipeError:
|
||||||
|
@ -195,11 +199,12 @@ def get_tyy_data(*args):
|
||||||
sc = None
|
sc = None
|
||||||
raise ParseError(f"采集器连接超时-{str(e)}")
|
raise ParseError(f"采集器连接超时-{str(e)}")
|
||||||
|
|
||||||
connect_and_send()
|
with sc_lock:
|
||||||
resp = sc.recv(1024)
|
connect_and_send()
|
||||||
res = handle_bytes(resp)
|
resp = sc.recv(1024)
|
||||||
# myLogger.error(res)
|
res = handle_bytes(resp)
|
||||||
if isinstance(res, str):
|
# myLogger.error(res)
|
||||||
raise ParseError(f'采集器返回数据错误-{res}')
|
if isinstance(res, str):
|
||||||
else:
|
raise ParseError(f'采集器返回数据错误-{res}')
|
||||||
return res
|
else:
|
||||||
|
return res
|
||||||
|
|
Loading…
Reference in New Issue