我有一些代码如下所示:
for item in list:
<bunch of slow python code, depending only on item>
我想通过并行循环来加快速度。通常情况下,multiprocessing
模块非常适合这一点(参见this question的答案),但它是在python2.6中添加的,我只能使用2.4。
在python2.4中并行化python循环的最佳方法是什么?
发布于 2013-07-26 00:25:44
您可能正在寻找“叉”,这将使它更容易使用特定的项目。
但是,您的for循环需要看起来有点不同--您想要在叉返回零时立即退出。
import os
L = ["a", "b", "c"]
for item in L:
pid = os.fork()
if pid == 0: break
else: print "Forked:", pid
if pid != 0: print "Main Execution, Ends"
else: print "Execution:", item
发布于 2013-07-26 00:09:08
我不熟悉使用python2.4,但您是否尝试过使用subprocess.Popen并只生成新的进程?
from subprocess import Popen
for x in range(n):
processes.append(Popen('python doWork.py',shell = True))
for process in processes:
process.wait()
https://stackoverflow.com/questions/17870778
复制相似问题