DO NOT terminate python subprocess when script ends

I've seen a ton of opposite questions that I find weird because I can't keep my subprocess from closing, but is there a way to call subprocess.Popen and make sure the process keeps running after the python script call exits?

My code looks like this:

dname = os.path.dirname(os.path.abspath(__file__))
script = '{}/visualizerUI.py'.format(dname)
self.proc = subprocess.Popen(['python', script, str(width), str(height), str(pixelSize)], stdout=subprocess.PIPE)

      

This opens the process just fine, but when I close my script (either because it exits or with Ctrl + C) it also closes the visualizerUI.py subprocess, but I want it to stay open. Or at least there is an option.

What am I missing?

+3


source to share


2 answers


Remove stdout = subprocess.PIPE and add shell = True so that it is spawned in a subshell that can be detached.



+2


source


Another variant:

import os
os.system("start python %s %s %s %s" % (script, str(width), str(height), str(pixelSize)))

      

To start a new python script in a new process with a new console.



Edit: just saw that you were working on a Mac, so I doubt it would work for you.

What about:

import os
import platform

operating_system = platform.system().lower()
if "windows" in operating_system:
    exe_string = "start python"
elif "darwin" in operating_system:
    exe_string = "open python"
else:
    exe_string = "python"
os.system("%s %s %s %s %s" % (exe_string, script, str(width),
          str(height), str(pixelSize))))

      

+1


source







All Articles