1

I am using for loop to open multiple cmd, where i can run python script in each cmd.

#test.py
import time
print("test.py started...)
time.sleep(1)
print(test.py finished...)
# test.py is same folder.
import subprocess
cmd_2 = "python test.py"
for i in range(3):
    b = subprocess.Popen(["start", "/wait", "cmd.exe", "/k", cmd2], shell=True)
    (output, err) = b.communicate()
    print(output)
    print(err)

The main problem is: code is waiting at b.communicate() line and waiting for to close the current instance of cmd (to kill the cmd completely). Is there any way where we don't have to wait to close/terminate the cmd and just open next cmd and run python code?

2
  • Wanting to run each subprocess in a window of its own is a common beginner request, but ultimately, many of those are resolved by deciding not to want that. Why do you need the subprocesses to be visible to the user? Commented Sep 25, 2022 at 8:31
  • test.py is hanging and i want to open a new cmd when test.py is hanged. So ultimate goal is open a new cmd and run test.py in this new opened cmd. Commented Sep 25, 2022 at 14:55

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for response @Ahmed AEK. basically test.py is hangling, and I want to run tets.py when it hanged. So appending process to list and run in loop will not solve problem. Is there any way whenever we want, we can open cmd and it run independently from main program (main is our code, other process is test.py).
@OneTouchForHeight it depends on what you want from test.py, if only its existence matters and you aren't reading anything from it then there is no reason to call communicate, as communicate will block until the other process ends, if you are however reading something from test.py stdout then this is where it gets complicated, and you'll have to have a thread reading from it in the background as shown in this answer
@OneTouchForHeight you really should've mentioned the hanging problem in the question or created a test.py that actually hangs ... the question was nowhere near your actual problem.
I used time.sleep(1000) which behave like it is hanged: I asked a new similar question: new question
You can try answering this question. That will help me a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.