首页 > 解决方案 > 如何在 Python 中对 3 个参数函数使用多处理

问题描述

我有 3 种大小相同的文件(每种类型大约 500 个文件)。我必须将这些文件交给一个函数。我怎样才能使用多处理?这些文件是 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 作为函数的参数。文件的起始名称可能会有所不同,但它是这种格式。

代码如下:

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 是主函数,它是从其他代码调用的。在这个函数中,我们有 color_img、depth_imp 和 mat,它们是文件夹中三种不同类型的文件。这三个文件作为 depth_rgb_registration 函数的参数给出。在此函数中,一些必需的值存储在 gait_list1 中,然后将其存储在每个文件集的 excel 文件中。

上面的这个循环是有效的,但是根据文件的数量,运行大约需要 20-30 分钟。所以我想使用 Multiprocessing 并减少总时间。

我通过查看一些示例尝试了多处理,但我无法理解如何将这 3 个文件作为参数。我知道在这里使用字典是不正确的,我在下面使用过,但是有什么可以替代的呢?即使是异步多处理,也没问题。我什至想过使用 GPU 来运行该功能,但是当我阅读时,将数据加载到 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

标签: pythonpython-3.xmultiprocessing

解决方案


我没有详细查看代码(它太长了),但是如果可以独立评估将发送到您的函数的带有 3 个参数的参数组合(在函数本身之外),您可以简单地使用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]

或者,如果您的函数可以重铸为一个接受单个参数(元组)并从该参数扩展为所需的单独变量的函数,那么您可以使用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))


推荐阅读