简体   繁体   中英

Run Python scripts in parallel and wait for all to finish before executing more parallel scripts

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?

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

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

I think it should work like this:

(
    start python C:\myfolder\1.py
    start python C:\myfolder\2.py
    start python C:\myfolder\3.py
) | pause

For an explanation see this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM