如何使用多重处理来加速以下功能?

ano*_*non 6 python numpy python-3.x python-multiprocessing

我有以下for循环:

for j in range(len(a_nested_list_of_ints)):
    arr_1_, arr_2_, arr_3_ = foo(a_nested_list_of_ints[j])
    arr_1[j,:] = arr_1_.data.numpy()
    arr_2[j,:] = arr_2_.data.numpy()
    arr_3[j,:] = arr_3_.data.numpy()
Run Code Online (Sandbox Code Playgroud)

a_nested_list_of_ints嵌套的整数列表在哪里。但是,这需要很多时间才能完成。如何通过多处理对其进行优化?到目前为止,我尝试使用multiprocessing

p = Pool(5)
for j in range(len(a_nested_list_of_ints)):
    arr_1_, arr_2_, arr_3_ = p.map(foo,a_nested_list_of_ints[j])
    arr_1[j,:] = arr_1_.data.numpy()
    arr_2[j,:] = arr_2_.data.numpy()
    arr_3[j,:] = arr_3_.data.numpy()
Run Code Online (Sandbox Code Playgroud)

但是,我得到:

ValueError: not enough values to unpack (expected 3, got 2)
Run Code Online (Sandbox Code Playgroud)

这里:

    arr_1_, arr_2_, arr_3_ = p.map(foo,a_nested_list_of_ints[j])
Run Code Online (Sandbox Code Playgroud)

是否知道如何使上述操作更快?我什至也尝试过使用starmap,但它不能正常工作。

小智 0

这是我经常使用的多处理实现。在本例中,它会将您的列表拆分a_nested_list_of_ints为您拥有的多个核心。foo然后,它在每个拆分列表上运行您的函数,每个核心一个列表。

def split_list(all_params, instances):
    return list(np.array_split(all_params, instances))

# split the list up into equal chucks for each core
n_proc = multiprocessing.cpu_count()
split_items = split_list(to_calc, n_proc)

# create the multiprocessing pool
pool = Pool(processes=n_proc)
all_calcs = []
for i in range(n_proc):
    # the arguments to the foo definition have to be a tuple - (split[i],)
    async_calc = pool.apply_async(foo, (split_items[i],))
    all_calcs.append(async_calc)

pool.close()
pool.join()

# get results
all_results = []
for result in all_calcs:
    all_results += result.get()

print(all_results)
Run Code Online (Sandbox Code Playgroud)