you don't need to wait for stdout to be read before you start the next process, you can start all processes in a loop then read them in a separate loop.
and you should also replace "/K" with "/C" to make the cmd close after it finishes execution and not hang waiting for you to close it manually, if you keep the "/K" all the cmd panels will stay open, and you will have 3 cmd windows open at the same time.
# test.py is same folder.
import subprocess
cmd_2 = "python test.py"
processes = []
for i in range(3):
processes.append(subprocess.Popen(["start", "/wait", "cmd.exe", "/C", cmd_2], shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE, text=True))
for i in range(3):
(output, err) = processes[i].communicate()
print("-----stdout----")
print(output)
print("-----stderr----")
print(err)
this will print nothing because cmd.exe
doesn't itself return the output to stdout ...
so why are you launching cmd.exe
anyway ? you are already passing shell=True
, so you have access to python.exe
through cmd, and you can read the output from it directly as follows.
import subprocess
cmd_2 = "python test.py"
processes = []
for i in range(3):
processes.append(subprocess.Popen(cmd_2, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE, text=True))
for i in range(3):
(output, err) = processes[i].communicate()
print("-----stdout----")
print(output)
print("-----stderr----")
print(err)