多处理池示例

发布于 2024-10-06 18:40:17 字数 628 浏览 2 评论 0原文

我正在尝试学习如何使用 multiprocessing,并发现 以下示例

我想按如下方式对值进行求和:

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把昨日还给我 2024-10-13 18:40:17

如果你要像这样使用 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

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.

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
青朷 2024-10-13 18:40:17

这是我在 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]"

它很简单,甚至我都能理解。
注意 result.get() 是触发计算的因素。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文