我正在从不断在套接字发送数据的设备读取数据,并且正在将多处理队列用作服务队列。该代码取决于硬件,但本质上是:
def save_package(dirs, data_queue, end_queue):
fp = None
prev_yaw = -100
while end_queue.empty():
if data_queue.empty():
#print 's_p passed'
pass
else:
msg = data_queue.get()
data = msg['data']
ts = msg['time']
yaw = msg['yaw']
if yaw != prev_yaw: # Here is where it stops writing
if fp is not None:
fp.close()
file_fmt = os.path.join(dirs, str(yaw))
path = str(file_fmt) + '.bin'
fp = open(path, 'ab')
else:
fp.write('%.6f' % ts)
fp.write(data)
prev_yaw = yaw
def capture(port, data_queue, end_queue, dirs):
soc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
soc.bind(('', port))
cnt = 0
take_points = 1000
yaw = 0
try:
while True:
if cnt == take_points: # Here is where it stops writing
yaw += 100
cnt = 0
else:
cnt += 1
data = soc.recv(2000)
if not end_queue.empty():
soc.close()
os.system('kill $PPID')
os._exit(2)
return
if len(data) > 0:
assert len(data) == 1206, len(data)
data_queue.put({'data': data, 'time': time.time(), 'yaw': yaw})
if __name__ == "__main__":
dirs = '..'
processA = Process(target = capture, args = (PORT, DATA_QUEUE, END_QUEUE, dirs))
processA.start()
processB = Process(target = save_package, args = (dirs, DATA_QUEUE, END_QUEUE))
processB.start()
while True:
while (processA.is_alive() == True or processB.is_alive() == True):
os._exit(2)
os.system('kill $PPID')
quit()
该代码应该为偏航变量的每个值将数据划分为1000个点,但是数据存在一定的“混合”-我可以在视觉上和根据时间戳看到它们。似乎每个文件的前77个数据包(第一个数据包除外)来自上一个偏航,并且与名称不匹配。
由于该数字异常地恒定,我希望有人能发现我这边的一些基本错误,因为根据制造商的说法,它不可能是设备。
是否有一种可以使人们看到的数据混合在一起的方法? 谢谢您的帮助。
编辑:我发现这个答案似乎也有相同的问题:Delay when receiving UDP packets in Python in real-time 如果这看起来像是同样的问题,那么有人可以解释一下2000缓冲区,1206消息长度和77 pakcets延迟之间的计算吗?