Python multiprocessing.Pool 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4413821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:39:05  来源:igfitidea点击:

multiprocessing.Pool example

pythonmultiprocessing

提问by litd

I'm trying to learn how to use multiprocessing, and found the following example.

我正在尝试学习如何使用multiprocessing,并找到了以下示例

I want to sum values as follows:

我想总结如下值:

from multiprocessing import Pool
from time import time

N = 10
K = 50
w = 0

def CostlyFunction(z):
    r = 0
    for k in xrange(1, K+2):
        r += z ** (1 / k**1.5)
    print r
    w += r
    return r

currtime = time()

po = Pool()

for i in xrange(N):
    po.apply_async(CostlyFunction,(i,))
po.close()
po.join()

print w
print '2: parallel: time elapsed:', time() - currtime

I can't get the sum of all r values.

我无法获得所有 r 值的总和。

回答by Justin Peel

If you're going to use apply_async like that, then you have to use some sort of shared memory. Also, you need to put the part that starts the multiprocessing so that it is only done when called by the initial script, not the pooled processes. Here's a way to do it with map.

如果您打算像这样使用 apply_async,那么您必须使用某种共享内存。此外,您需要放置启动多处理的部分,以便它仅在由初始脚本调用时完成,而不是在池化进程中调用。这是一种使用地图进行操作的方法。

from multiprocessing import Pool
from time import time

K = 50
def CostlyFunction((z,)):
    r = 0
    for k in xrange(1, K+2):
        r += z ** (1 / k**1.5)
    return r

if __name__ == "__main__":
    currtime = time()
    N = 10
    po = Pool()
    res = po.map_async(CostlyFunction,((i,) for i in xrange(N)))
    w = sum(res.get())
    print w
    print '2: parallel: time elapsed:', time() - currtime

回答by GuySoft

Here is the simplest example I found in the python example documentation:

这是我在python 示例文档中找到的最简单的示例:

from multiprocessing import Pool

def  f(x):
    return x*x

if __name__ == '__main__':
    pool = Pool(processes=4)              # start 4 worker processes
    result = pool.apply_async(f, [10])    # evaluate "f(10)" asynchronously
    print result.get(timeout=1)           # prints "100" unless your computer is *very* slow
    print pool.map(f, range(10))          # prints "[0, 1, 4,..., 81]"

It was simple enough even I could understand it.
Note result.get()is what triggers the computation.

这很简单,连我都能理解。
注意result.get()是什么触发了计算。