并行运行Python脚本,并等待所有脚本完成,然后再执行更多并行脚本 [英] Run Python scripts in parallel and wait for all to finish before executing more parallel scripts

查看:387
本文介绍了并行运行Python脚本,并等待所有脚本完成,然后再执行更多并行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要并行执行我的Python脚本,因此我使用以下批处理文件:

I need to execute my Python scripts in parallel so I use the following batch file for it:

start python C:\myfolder\1.py
start python C:\myfolder\2.py
start python C:\myfolder\3.py

它工作正常,但是现在我需要在上述前三个完成之后并行运行三个脚本.如何在同一批处理文件中指定它?

It works fine, but now I need to run three more scripts in parallel AFTER the above first three finish. How can I specify it in the same batch file?

推荐答案

您可以在python中轻松完成此操作,而无需使用Windows批处理命令.

You can easily do this in python without using windows batch commands.

您可以使用subprocess库同时运行外部脚本,然后等待它们全部完成.

You can use the subprocess library to run external scripts simultaneously and then wait for them all to complete.

processes = []
scripts = [
    r'C:\myfolder\1.py',
    r'C:\myfolder\2.py',
    r'C:\myfolder\3.py',
]
for script in scripts:
    p = subprocess.Popen(['python', script])
    processes.append(p)

for p in processes:
    p.wait()

# Run other processes here

这篇关于并行运行Python脚本,并等待所有脚本完成,然后再执行更多并行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆