Search code examples
pythoncmdsubprocess

subprocess.popen leaves out ghost sessions


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)

Solution

    • For one, you do probably want to hang on to the Popen objects and eventually wait for them to quit.
    • Second, you don't need a batch file to use an Anaconda environment's interpreter; just use the python.exe within that Scripts (or bin) directory; similarly, use sys.executable to refer to the current Python interpreter's executable.
    • You also don't need 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()