首页 > 编程语言 > 详细

Python管道进行数据的吞吐处理

时间:2019-12-28 11:24:31      阅读:80      评论:0      收藏:0      [点我收藏+]
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)

Python管道进行数据的吞吐处理

原文:https://www.cnblogs.com/mikew/p/12111032.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!