问题
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