Python管道进行数据的吞吐处理
2024-10-10 08:46:45
import multiprocessing
import random
import time
import datetime
import struct
import os import getFile # 76(28) + (2048 + 16) * 512 + 4 frame_flag_0 = 0x0000000000000001 # 8 byte
frame_flag_1 = 0x0000000000000002 # 8 byte
frame_flag_2 = 0x0000000000000003 # 8 byte
frame_flag_9 = 0x00000001 # 4 byte dir_name = 'Z:/'
file_out = 'Z:/312.dat' def producer(pipe):
print('start time: ' + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
linestr = getFile.getFileList(dir_name, '_312_') for i in range(0, len(linestr)):
file_in = linestr[i]
f_in = open(file_in, 'rb')
f_in.seek(109)
while True:
buff = f_in.read(9)
buff = f_in.read(1792)
if buff:
pipe.send_bytes(buff)
else:
f_in.close()
break pipe.close() def consumer(pipe):
f_out = open(file_out, 'xb')
mm = 0
while True:
try: item = pipe.recv_bytes(1792)
if item:
if mm % 512 == 0: # 开始的首帧
f_out.write(struct.pack('>3Q', frame_flag_0, frame_flag_1, frame_flag_2))
f_out.write(struct.pack('>I', frame_flag_9))
f_out.write(item)
f_out.write(struct.pack('>I', 0x00000000) * 68) # 272 byte : 2048 - (896*2) + 16
mm = 0
elif mm % 512 == 511: # 结尾的结束帧
f_out.write(item)
f_out.write(struct.pack('>I', 0x00000000) * 68) # 272 byte
f_out.write(struct.pack('>I', 0x00000000)) # 4 byte 包尾
f_out.flush()
else:
f_out.write(item)
f_out.write(struct.pack('>I', 0x00000000) * 68) # 272 byte
mm += 1 except EOFError:
f_out.write(struct.pack('>I', 0x00000000) * 516 * (512 - mm)) # (2048 + 16)/4 = 516
f_out.write(struct.pack('>I', 0x00000000)) # 4 byte 包尾
f_out.close()
print('end time: ' + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
break if __name__ == "__main__":
pipe = multiprocessing.Pipe()
process_producer = multiprocessing.Process(target = producer, args = (pipe[0],))
process_consumer = multiprocessing.Process(target = consumer, args = (pipe[1],))
process_producer.start()
process_consumer.start()
pipe[0].close()
process_producer.join()
process_consumer.join() print('OK')
最新文章
- Webform 文件上传、 C#加图片水印 、 图片验证码
- 关于e^PI>;PI^e
- FZU 2216 The Longest Straight(最长直道)
- extjs 学习笔记(二)
- 学习使用Free RTOS ,移植最新的STM32 v3.5固件库
- on事件绑定阻止冒泡的问题
- 洛谷U4727 小L 的二叉树
- Spring 对缓存的抽象
- 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能
- [Sdoi2009]Elaxia的路线
- android 系统dialog的应用
- DOS:第二天
- Oracle 12C 密码文件问题 ORA-01017: invalid username/password; logon denied
- 02_计算机网络的OSI七层(应表会传网数物)
- TCP建立与断开连接、socket通讯模板
- C语言复习---找出报数最后一人
- pyqt5-顶层窗口特定操作-图标和标题和不透明度
- eclipse中创建DataBase Connections
- SYSAUX表空间如何清理
- 关于ubuntu下看视频中文字幕乱码的问题