简体   繁体   中英

Run multiple bash commands simultaneously in Python

I want to create a python script that can start multiple instances of a bash script.

I have tried

import subprocess

    commands = '''
    bashcmd1
    bashcmd2
    bashcmd3
    '''

    process = subprocess.Popen('/bin/bash', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    out, err = process.communicate(commands)
    print out

But it stops at bashcmd2 and continues only if i hit CTRL+C .

you're just running one bash with 3 commands in it.

If the commands aren't setting variables or depending from each other (else you would not be able to parallelize them), maybe you could create 3 subprocess.Popen instances instead:

commands = '''
bashcmd1
bashcmd2
bashcmd3
'''

for process in [subprocess.Popen(['/bin/bash', '-c', line], stdout=subprocess.PIPE) 
                for line in commands.split("\n") if line]:  # filter out blank lines
    out, err = process.communicate() # or just rc = process.wait()
    # print out & err

that command first create a list comprehension of Popen objects (list not generator so the processes start immediately), then perform a communicate to wait for completion (but other processes are running in the meanwhile)

The upside is that you can apply this technique to any script containing commands, and you don't need to use the shell & capability (more portable, including Windows provided you're using ["cmd","/c" prefix instead of bash )

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