0

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?

  • 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? – tripleee Sep 25 '22 at 08: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. – OneTouchForHeight Sep 25 '22 at 14:55

1 Answers1

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)
Ahmed AEK
  • 8,584
  • 2
  • 7
  • 23
  • 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 Sep 25 '22 at 14:51
  • @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](https://stackoverflow.com/questions/73763291/supplying-input-in-subprocess-and-print-the-inputs-along-with-the-outputs-pytho/73763539#73763539) – Ahmed AEK Sep 25 '22 at 16:28
  • @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. – Ahmed AEK Sep 25 '22 at 16:34
  • I used time.sleep(1000) which behave like it is hanged: I asked a new similar question: [new question](https://stackoverflow.com/questions/73845689/terminate-a-python-program-when-it-hanged-using-subprocess-python/73847424#73847424) – OneTouchForHeight Sep 26 '22 at 03:27
  • You can try answering this question. That will help me a lot. – OneTouchForHeight Sep 26 '22 at 03:27