I'm learning python and could use some help. The following code executes 4 subprocess commands. Each command is executed one after the next with all of it's output and errors being displayed. How to I preserve the behavior of waiting and displaying all output, but execute multiple commands concurrently? I've looked at running several system commands in parallel which seems to approximate what I think I'm going for, but am not sure how to make it work and maintain the output to stdout for each process.
def execute(command):
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
nextline = process.stdout.readline()
if nextline == '' and process.poll() != None:
break
sys.stdout.write(nextline)
sys.stdout.flush()
output = process.communicate()[0]
returncode = process.returncode
if (returncode == 0):
return output
else:
raise Exception("%s %s %s" % (command, returncode, output))
execute(cmd1)
execute(cmd2)
execute(cmd3)
execute(cmd4)