multiprocessing (using concurrent.futures) appending list

ぐ巨炮叔叔 提交于 2021-02-11 17:51:54

问题


I am trying to append values to a list. But, as it's multiprocessing, the list is ending up with just one value finally. Is there a way where I can append sizes of all the images to the list rather than just one?

import cv2
import concurrent.futures
import os
length = []
def multi_proc(image):
    name = image[0:-4]
    im = cv2.imread(image)
    final_im = cv2.resize(im, (100,100))
    cv2.imwrite(name+"rs"+".png", final_im)
    l = im.shape
    print(l)
    length.append(l)

with concurrent.futures.ProcessPoolExecutor() as executor:
    sound_files = [f for f in os.listdir('.') if f.endswith('.png')]
    executor.map(multi_proc, sound_files)

回答1:


import cv2
import concurrent.futures
import os
length = []

def multi_proc(image):
    name = image[0:-4]
    im = cv2.imread(image)
    final_im = cv2.resize(im, (100,100))
    cv2.imwrite(name+"rs"+".png", final_im)
    l = im.shape
    print(l)
    return l


def app_img(l):
    length.append(l)

def start_processing():
    with concurrent.futures.ProcessPoolExecutor() as executor:
        sound_files = [f for f in os.listdir('.') if f.endswith('.png')]
        breakpoint()
        future_proc = {executor.submit(multi_proc, f): f for f in sound_files}
        for future in concurrent.futures.as_completed(future_proc):
            app_img(future.result())


if __name__ == "__main__":
    start_processing()
    print(len(length))

input: 2 png files output:

(225, 225, 3)
(313, 161, 3)
2



回答2:


You can just use nested lists. Instead of length.append(l), try length.append([im, final_im, l]).



来源:https://stackoverflow.com/questions/60018676/multiprocessing-using-concurrent-futures-appending-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!