我一直在尝试并行管理一系列批处理文件进程,同时有子进程的相关组。我希望得到的是能够并行运行group1的所有进程,然后等待所有进程完成后再运行group2,依此类推。想象一下一系列的过程,其中每个过程都是一个单独的现有批处理文件(batch_i.bat)
基于对多进程模块的理解,我有以下代码,因此我希望在调用最终打印命令时,所有日志文件都是完整的,可以打印所有数字。但是,我注意到python代码没有完成批处理过程而成功完成。
import multiprocessing as mp
import subprocess
def worker(cmdlist, log):
with open(log, 'w') as logfile:
p = subprocess.Popen(cmdlist, stderr=logfile, stdout=logfile)
# return p.returncode
# --------------------------------------------
# Main Process (Group 1)
# --------------------------------------------
if __name__ == '__main__':
group1 = [batch_1 , batch_2 , batch_3 , ..., batch_10]
group2 = [batch_11, batch_12, batch_13, ..., batch_20]
group3 = [batch_21, batch_22, batch_23, ..., batch_30]
# Multi-Core Exec
all_process = group1
all_results = []
pool = mp.Pool(processes=4)
for myProcess in all_process:
print("Starting Process: %s" %myProcess)
log = os.path.splitext(myProcess)[0] + ".log"
res = pool.apply_async(worker, args=[myProcess, log])
all_results.append(res)
pool.close()
pool.join()
print("All sub-processes completed")
for res in all_results:
res.get()
print("All sub-processes completed: %s" % [res.successful() for res in all_results])
# --------------------------------------------
# call group 2 and wait for completion
# --------------------------------------------
....
# --------------------------------------------
# call group 3 and wait for completion
# --------------------------------------------
...
其余代码将调用group2中所有依赖于group 1的完成的进程,依此类推
在这种情况下,批处理文件只是一个示例,除了打印大量数字外什么都不做,我将循环重复了几次,以确保批处理文件花费足够的时间才能完成。
@echo off
echo Start of Loop
for /L %%n in (1,1,40000) do echo %%n
for /L %%n in (1,1,40000) do echo %%n
for /L %%n in (1,1,40000) do echo %%n
for /L %%n in (1,1,40000) do echo %%n
echo End of Loop
输出如下:
> *** Running Base Cases: ***
> on 4 CPUs Process: C:\Users\mamo8001\Project\Clustering\01 Codes\testNum.bat Process: C:\Users\mamo8001\Project\Clustering\01
> Codes\testNum2.bat Process: C:\Users\mamo8001\Project\Clustering\01
> Codes\testNum3.bat Process: C:\Users\mamo8001\Project\Clustering\01
> Codes\testNum4.bat Process: C:\Users\mamo8001\Project\Clustering\01
> Codes\testNum.bat Process: C:\Users\mamo8001\Project\Clustering\01
> Codes\testNum2.bat Process: C:\Users\mamo8001\Project\Clustering\01
> Codes\testNum3.bat Process: C:\Users\mamo8001\Project\Clustering\01
> Codes\testNum4.bat
> All sub-processes completed
> All sub-processes completed: [True, True, True, True, True, True, True,
> True]
>
> Process finished with exit code 0
在打印最后两行时,我注意到日志文件没有打印出完整的数字列表,即批处理尚未完成
答案 0 :(得分:2)
问题在于您的工作人员不等待其子流程退出。在工作器中的p.wait()
调用之后添加p = subprocess.Popen()
。
答案 1 :(得分:0)
使用8个批处理文件,每个文件只有一个 for循环到40000,直到运行Popen
作为上下文管理器,我得到的结果都是相同的。
def worker(cmdlist, log):
with open(log, 'w') as logfile:
with subprocess.Popen(cmdlist, stderr=logfile, stdout=logfile) as p:
pass
# return p.returncode
然后,直到所有cmd窗口关闭,最后两个打印语句才打印。每个日志文件都有所有数字以及循环行的开始/结束。
用作上下文管理器的文档说, it 等到该过程完成。
如果您使用的是Python 3.5+,则文档会说使用subprocess.run()
而不是Popen
,而.run()
文档则明确表示它会等到命令完成-我无法测试,我有Python 3.4。
批处理文件为
echo off
echo Start of Loop
for /L %%n in (1,1,40000) do echo %%n
echo End of Loop