![](/img/trans.png)
[英]Clarification on the `processes` argument for Python's `multiprocessing.Pool`
[英]Python's multiprocessing.Pool spawns new processes
我对多处理模块有一个简单的问题。 我正在使用multiprocessing.Pool的map()
函数来加快本地计算机上自写代码的执行速度。 但是,此代码在迭代循环中运行,每次循环迭代时,我都会在计算机中生成其他Python进程。 (这是一个问题,因为系统缓慢地停止运行)。 这是一个简单的例子:
from multiprocessing import Pool
import os
nthreads = 2
for ii in xrange(5):
pool = Pool(processes=nthreads) # (in my code, Pool is inside a pickleable function.)
runningProcesses = os.popen('ps | grep ython').readlines()
nproc = len(runningProcesses)
print "After iteration %i there were %i Python processes running!" % (ii, nproc)
输出为:
After iteration 0 there were 5 Python processes running!
After iteration 1 there were 7 Python processes running!
After iteration 2 there were 9 Python processes running!
After iteration 3 there were 11 Python processes running!
After iteration 4 there were 13 Python processes running!
我应该如何安排我的代码以避免产生许多新的Python进程? 我正在运行具有多处理v0.70a1的Python 2.7.6,并且在运行OSX 10.8.5的4核MacBook Pro上。
将pool = Pool(processes=nthreads)
放在for
循环上方
正如评论中所讨论的那样,池中的工作进程并未关闭/加入,因此它们永远不会终止。 此处的最高答案显示了在不再需要池时如何清理它: Python多处理池,连接; 等不及要继续?
附带说明一下,如果要创建大量的工作程序并使用它们执行非常短/快速的工作,则可能会发现性能下降-操作系统创建和销毁进程会产生开销。 如果是这种情况,那么您应该考虑在整个应用程序中使用一个池。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.