Run Process and Don't Wait

Viewed 99708

I'd like to run a process and not wait for it to return. I've tried spawn with P_NOWAIT and subprocess like this:

app = "C:\Windows\Notepad.exe"
file = "C:\Path\To\File.txt"

pid = subprocess.Popen(
    [app, file], 
    shell=True, 
    stdin=subprocess.PIPE, 
    stdout=subprocess.PIPE,
).pid

However, the console window remains until I close Notepad. Is it possible to launch the process and not wait for it to complete?

4 Answers

You are capturing input and output to the program so your program will not terminate as long as it keeps those file descriptors open. If you want to capture, you need to close the file descriptors. If you don't want to capture:

app = "C:\Windows\Notepad.exe"
file = "C:\Path\To\File.txt"

pid = subprocess.Popen([app, file]).pid
Related