63

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?

7
  • 1
    Are you using python.exe or pythonw.exe?
    – S.Lott
    Commented Aug 18, 2010 at 19:48
  • 6
    Apparently python is smart enough to realize those backslashes aren't escape characters, nevertheless, I would avoid doing that by using r'C:\path' or even better 'C:/path'
    – Nick T
    Commented Aug 18, 2010 at 20:40
  • @NickT or just single quote instead of double Commented Nov 13, 2011 at 20:51
  • 1
    @aking1012 single vs. double quoted strings are parsed differently? Did not know
    – Nick T
    Commented Nov 28, 2011 at 19:56
  • 5
    @aking1012: You are; Python does not treat single and double quotes differently.
    – icktoofay
    Commented Jun 10, 2012 at 2:37

4 Answers 4

80

This call doesn't wait for the child process to terminate (on Linux). Don't ask me what close_fds does; I wrote the code some years ago. (BTW: The documentation of subprocess.Popen is confusing, IMHO.)

proc = Popen([cmd_str], shell=True,
             stdin=None, stdout=None, stderr=None, close_fds=True)

Edit:

I looked at the the documentation of subprocess, and I believe the important aspect for you is stdin=None, stdout=None, stderr=None,. Otherwise Popen captures the program's output, and you are expected to look at it. close_fds makes the parent process' file handles inaccessible for the child.

3
  • 1
    I need the win32 DETACHED_PROCESS flag otherwise it hangs on Windows for me.
    – MarcH
    Commented Jul 23, 2012 at 13:31
  • 5
    According to [1] shell=True may be a security hazard and if shell is True, it is recommended to pass args as a String (I had problems with incorrectly passed args using a sequence and shell=True. Although my args sequence matched shlex output). I'm pretty sure shell=True is not needed for detaching (tested only on Linux). Commented Jun 14, 2013 at 9:04
  • 3
    Since most of the answer's args are their defaults, this can probably be simplified to proc = Popen([cmd_str]) (notepad probably doesn't need shell).
    – idbrii
    Commented Jan 22, 2018 at 17:57
32

I finally got this to work. I'm running "Python 2.6.6 (r266:84297, Aug 24 2010, 18:13:38) [MSC v.1500 64 bit (AMD64)] win32". Here's how I had to code it:

from subprocess import Popen
DETACHED_PROCESS = 0x00000008
cmd = [
    sys.executable,
    'c:\somepath\someprogram.exe',
    parm1,
    parm2,
    parm3,
]
p = Popen(
    cmd, shell=False, stdin=None, stdout=None, stderr=None,
    close_fds=True, creationflags=DETACHED_PROCESS,
)

This turns off all piping of standard input/output and does NOT execute the called program in the shell. Setting 'creationflags' to DETACHED_PROCESS seemed to do the trick for me. I forget where I found out about it, but an example is used here.

2
  • 3
    the proper way to tokenize is with shlex Commented Nov 13, 2011 at 20:47
  • 11
    creationflags is windows only Commented Jan 2, 2013 at 15:03
5

I think the simplest way to implement this is using the os.spawn* family of functions passing the P_NOWAIT flag.

This for example will spawn a cp process to copy a large file to a new directory and not bother to wait for it.

import os
os.spawnlp(os.P_NOWAIT, 'cp', 'cp', '/path/large-file.db', '/path/dest')
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.