我想在多个核心上同时运行一个串行程序,我需要多次(循环)。 我使用subprocess.Popen通过将作业数量限制为可用处理器数量来分配处理器上的作业。我将作业添加到列表中,然后使用poll()检查作业是否完成,我将其从列表中删除并继续提交,直到完成作业总数。
我一直在寻找网络,并发现了一些有趣的脚本来做到这一点,并推出了我的改编版本:
nextProc = 0
processes = []
while (len(processes) < limitProc): # Here I assume that limitProc < ncores
input = filelist[nextProc]+'.in' # filelist: list of input file
output = filelist[nextProc]+'.out' # list of output file
cwd = pathlist[nextProc] # list of paths
processes.append(subprocess.Popen(['myProgram','-i',input,'-screen',output],cwd=cwd,bufsize=-1))
nextProc += 1
time.sleep(wait)
while (len(processes) > 0): # Loop until all processes are done
time.sleep(wait)
for i in xrange(len(processes)-1, -1, -1): # Remove processes done (traverse backward)
if processes[i].poll() is not None:
del processes[i]
time.sleep(wait)
while (len(processes) < limitProc) and (nextProc < maxProcesses): # Submit new processes
output = filelist[nextProc]+'.out'
input = filelist[nextProc]+'.in'
cwd = pathlist[nextProc]
processes.append(subprocess.Popen(['myProgram','-i',input,'-screen',output],cwd=cwd,bufsize=-1))
nextProc += 1
time.sleep(wait)
print 'Jobs Done'
我在循环中运行此脚本,问题是执行时间从一步增加到另一步。以下是图表:http://i62.tinypic.com/2lk8f41.png
myProgram时间执行是不变的。 如果有人能解释我造成这种泄漏的原因,我会很高兴。
非常感谢, Begbi