python多进程数据处理

使用python3.4+的import concurrent.futures

首先定义函数处理函数,例如视频提帧的函数如下:

 

def function(json_path):
    key_frame_list = []
    with open(json_path) as fr_json:
        data = json.load(fr_json)
        video_filename = video_path + data['videoid']

        for i in range(len(data['shots'])):
            for target in data['shots'][i]['targets']:
                if target['category'] == 0:

                    result_path = extract_result_path + data['videoid']
                    if not os.path.exists(result_path):
                        os.mkdir(result_path)
                    key_frame_list.append(data['shots'][i]['keyframe'])
        key_frame_list = list(set(key_frame_list))
        key_frame_list.sort()
        cap = cv2.VideoCapture(video_filename)
        for frame_num in key_frame_list:
            cap.set(1,frame_num)
            rval, frame = cap.read()
            try:
                # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                # img = Image.fromarray(frame)
                # cv2.imshow('Frame',frame)
                cv2.imwrite(result_path+'/'+str(frame_num)+'.jpg',frame)

                ch = cv2.waitKey(5)
                if ch == 27:
                    break
            except:
                pass

    cap.release()

 

然后用多进程调用使用的函数。注意多进程的冲突问题,也就是只能多对多或者一对多(如果一个文件生成多个文件,需要在function函数中进行判断),不能多对一(如果多个文件同时写入一个文件的话会产生冲突)

 

最后是函数调用,先将需要处理的文件放到List当中,然后调用function函数


json_file=os.listdir(json_path)
json_filename_list=[]
# print(json_file)
# for json_filename in json_file:
#      json_all_filename=os.path.join(json_path,json_filename)
#      json_filename_list.append(json_all_filename)

for json_filename in json_file:
    json_all_filename = json_path+json_filename
    json_filename_list.append(json_all_filename)
    # print(json_all_filename)
    # print(type(json_all_filename))
    # json_all_filename=str(json_all_filename)
    # print(json_all_filename)
    #function(json_all_filename)#not use multiThread

with concurrent.futures.ProcessPoolExecutor() as executor:
     for image_file,result in zip(json_filename_list,executor.map(function,json_filename_list)):
         print("finish one video!!")

 

你可能感兴趣的:(python并行计算)