英文:
How to pass the index of the iterable when using multiprocessing pool
问题
I would like to call a function task()
in parallel N
times. The function accepts two arguments, one is an array and the second is an index to write the return result in to the array:
我想并行调用函数 task()
N
次。该函数接受两个参数,一个是数组,第二个是要将返回结果写入数组的索引:
def task(arr, index):
arr[index] = "some result to return"
To be explicit, the reason for the array is so I can process all the parallel tasks once they have completed. I presume this is ok?
明确一下,数组的原因是为了在并行任务完成后能够处理它们。我假设这样做没问题?
I have created a multiprocessing pool and it calls task()
:
我创建了一个多进程池,并在其中调用 task()
:
def main():
N = 10
arr = np.empty(N)
pool = Pool(os.cpu_count())
pool.map(task, arr)
pool.close()
# Process results in arr
However, the problem is because map()
is already iterable, how do I explicitly pass in the index? Each call to task()
should pass in 0, 1, 2.... N.
然而,问题在于 map()
已经是可迭代的,如何明确传递索引?每次调用 task()
应该传入 0、1、2.... N。
英文:
I would like to call a function task()
in parallel N
times. The function accepts two arguments, one is an array and the second is an index to write the return result in to the array:
def task(arr, index):
arr[index] = "some result to return"
To be explicit, the reason for the array is so I can process all the parallel tasks once they have completed. I presume this is ok?
I have created a multiprocessing pool and it calls task()
:
def main():
N = 10
arr = np.empty(N)
pool = Pool(os.cpu_count())
pool.map(task, arr)
pool.close()
# Process results in arr
However, the problem is because map()
is already iterable, how do I explicitly pass in the index? Each call to task()
should pass in 0, 1, 2.... N.
答案1
得分: 1
import multiprocessing as mp
import numpy as np
def task(index, arr):
print(index, arr)
if __name__ == '__main__':
N = 10
arr = np.empty(N)
with mp.Pool(mp.cpu_count()) as pool:
pool.starmap(task, enumerate(arr))
Output:
0 6.9180446290108e-310
1 6.9180446290108e-310
2 6.91804453329406e-310
3 6.91804425777776e-310
4 6.9180448957438e-310
5 6.9180105412701e-310
6 6.9180443068017e-310
7 6.91804453327193e-310
9 6.9180449088978e-310
8 6.91804436388567e-310
英文:
You can use:
import multiprocessing as mp
import numpy as np
def task(index, arr):
print(index, arr)
if __name__ == '__main__':
N = 10
arr = np.empty(N)
with mp.Pool(mp.cpu_count()) as pool:
pool.starmap(task, enumerate(arr))
Output:
0 6.9180446290108e-310
1 6.9180446290108e-310
2 6.91804453329406e-310
3 6.91804425777776e-310
4 6.9180448957438e-310
5 6.9180105412701e-310
6 6.9180443068017e-310
7 6.91804453327193e-310
9 6.9180449088978e-310
8 6.91804436388567e-310
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论