I want to launch multiple cmd and python files at the same time and not wait for response.
With the above solution, subprocess.popen
leaves out ghost session in Task Manager (Windows).
test.py
import time
time.sleep(5) # each subprocess lasts long enough to have 10 sessions active
test.cmd
call <anaconda_dir>/Scripts/activate.bat
python C:\Users\abc\Desktop\test.py
parent.py
import subprocess
p1 = "C:\\Users\\abc\\Desktop\\test.cmd"
p2 = "C:\\Users\\abc\\Desktop\\test.py"
for i in range(10):
print(i)
if i % 2 == 0:
subprocess.Popen(p1, shell=True)
else:
subprocess.Popen(p2, shell=True)
Popen
objects and eventually wait for them to quit.python.exe
within that Scripts
(or bin
) directory; similarly, use sys.executable
to refer to the current Python interpreter's executable.shell=True
.import subprocess
import sys
anaconda_python = "anaconda_dir/Scripts/python.exe" # TODO: correct this path
script = "C:\\Users\\abc\\Desktop\\test.py"
processes = []
for i in range(10):
if i % 2 == 0:
command = [sys.executable, script] # current interpreter
else:
command = [anaconda_python, script] # another interpreter
processes.append(subprocess.Popen(command))
for proc in processes:
proc.wait()