多处理池示例
我正在尝试学习如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你要像这样使用 apply_async ,那么你必须使用某种共享内存。此外,您需要放置启动多重处理的部分,以便仅在初始脚本调用时才完成,而不是池进程调用时完成。这是一种使用地图来完成此操作的方法。
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.
这是我在 python 示例中找到的最简单的示例文档:
它很简单,甚至我都能理解。
注意
result.get()
是触发计算的因素。Here is the simplest example I found in the python example documentation:
It was simple enough even I could understand it.
Note
result.get()
is what triggers the computation.