绘制池图以进行多处理Python [英] Plotting the pool map for multi processing Python
问题描述
如何使用python中的多处理工具运行多个进程池,在其中异步处理run1-3.我正在尝试为 run1,run2,run3
传递值(10,2,4),(55,6,8),(9,8,7)
分别?
How can I run multiple processes pool where I process run1-3 asynchronously, with a multi processing tool in python. I am trying to pass the values (10,2,4),(55,6,8),(9,8,7)
for run1,run2,run3
respectively?
import multiprocessing
def Numbers(number,number2,divider):
value = number * number2/divider
return value
if __name__ == "__main__":
with multiprocessing.Pool(3) as pool: # 3 processes
run1, run2, run3 = pool.map(Numbers, [(10,2,4),(55,6,8),(9,8,7)]) # map input & output
推荐答案
根据文档,您只需要使用方法 starmap
而不是 map
,
You just need to use method starmap
instead of map
, which, according to the documentation:
类似于
map()
,不同之处在于 iterable 的元素应该是可迭代的,并作为参数解压缩.
Like
map()
except that the elements of the iterable are expected to be iterables that are unpacked as arguments.
因此 [(1,2 ,,(3,4)]
的 iterable 会导致 [func(1,2),func(3,4)]
.
import multiprocessing
def Numbers(number,number2,divider):
value = number * number2/divider
return value
if __name__ == "__main__":
with multiprocessing.Pool(3) as pool: # 3 processes
run1, run2, run3 = pool.starmap(Numbers, [(10,2,4),(55,6,8),(9,8,7)]) # map input & output
print(run1, run2, run3)
打印:
5.0 41.25 10.285714285714286
注意
这是正确的工作方式,但是您不会发现对这种琐碎的工作程序功能使用多重处理会提高性能;实际上,由于创建池以及将参数和结果往返于一个地址空间与另一个地址空间之间的开销会降低性能.
This is the correct way of doing what you want to do, but you will not find that using multiprocessing for such a trivial worker function will improve performance; in fact, it will degrade performance due to the overhead in creating the pool and passing arguments and results to and from one address space to another.
这篇关于绘制池图以进行多处理Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!