[英]How to pass argument in multiprocessing function and how to use multiprocessing list?
[英]How to use Multiprocessing for 3 argument function in Python
我有 3 種大小相同的文件(每種類型大約 500 個文件)。 我必須將這些文件交給 function。 我怎樣才能使用多處理? 這些文件是 rgb_image: 15.png,16.png,17.png.... depth_img: 15.png, 16.png, 17.png 和 mat:15.mat, 16.mat, 17.mat...我必須使用 3 個文件 15.png、15.png 和 15.mat 作為 function 的參數。 文件的起始名稱可能會有所不同,但它是這種格式。
代碼如下:
def depth_rgb_registration(rgb, depth, mat):
required operation is performed here and
gait_list ( a list is the output of this function)
def display_fun(mat, selected_depth, selected_color, excel):
for idx, color_img in enumerate(color_lists):
for i in range(len(depth_lists)):
if color_img.split('.')[0] == depth_lists[i].split('.')[0]:
rgb = os.path.join(selected_color, color_img)
depth = os.path.join(selected_depth, sorted(depth_lists)[i])
m = sorted(mat_lists)[idx]
mat2 = os.path.join(mat, m)
abc = color_img.split('.')[0]
gait_list1 = []
fnum = int("".join([str(i) for i in re.findall("(\d+)", abc)]))
gait_list1.append(fnum)
depth_rgb_registration(rgb, depth,mat2)
gait_list2.append(gait_list1) #Output gait_list1 from above function
data1 = pd.DataFrame(gait_list2)
data1.to_excel(writer, index=False)
wb.save(excel)
在上面的代碼中,我們有 display_fun,它是主要的 function,它是從其他代碼調用的。 在這個 function 中,我們有 color_img、depth_imp 和 mat,它們是文件夾中的三種不同類型的文件。 這三個文件作為 arguments 到 depth_rgb_registration function 給出。 在這個 function 中,一些必需的值存儲在 gait_list1 中,然后存儲在 excel 文件中,用於每組文件。
上面的這個循環是有效的,但是根據文件的數量,運行大約需要 20-30 分鍾。 所以我想使用 Multiprocessing 並減少總時間。
我通過查看一些示例嘗試了多處理,但我無法理解如何將這 3 個文件作為參數。 我知道在這里使用字典是不正確的,我在下面使用過,但是有什么可以替代的呢? 即使是異步多處理,也沒問題。 I even thought of using GPU to run the function, but as I read, extra time will go in the loading of the data to GPU. 有什么建議么?
def display_fun2(mat, selected_depth, selected_color, results, excel):
path3 = selected_depth
path4 = selected_color
path5 = mat
rgb_depth_pairs = defaultdict(list)
for rgb in path4.iterdir():
rgb_depth_pairs[rgb.stem].append(rgb)
included_extensions = ['png']
images = [fn for ext in included_extensions for fn in path3.glob(f'*.{ext}')]
for image in images:
rgb_depth_pairs[image.stem].append(image)
for mat in path5.iterdir():
rgb_depth_pairs[mat.stem].append(mat)
rgb_depth_pairs = [item for item in rgb_depth_pairs.items() if len(item) == 3]
with Pool() as p:
p.starmap_async(process_pairs, rgb_depth_pairs)
gait_list2.append(gait_list1)
data1 = pd.DataFrame(gait_list2)
data1.to_excel(writer, index=False)
wb.save(excel)
def depth_rgb_registration(rgb, depth, mat):
required operation for one set of files
I did not look at the code in detail (it was too long), but provided that the combinations of arguments that will be sent to your function with 3 arguments can be evaluated independently (outside of the function itself), you can simply use Pool.starmap
:
例如:
from multiprocessing import Pool
def myfunc(a, b, c):
return 100*a + 10*b + c
myargs = [(2,3,1), (1,2,4), (5,3,2), (4,6,1), (1,3,8), (3,4,1)]
p = Pool(2)
print(p.starmap(myfunc, myargs))
返回:
[231, 124, 532, 461, 138, 341]
或者,如果您的 function 可以重鑄為 function ,它接受單個參數(元組)並將其擴展為所需的單獨變量,那么您可以使用Pool.map
:
def myfunc(t):
a, b, c = t # unpack the tuple and carry on
return 100*a + 10*b + c
...
print(p.map(myfunc, myargs))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.