detached subprocess

Robin Becker robin at reportlab.com
Mon May 23 10:52:25 EDT 2005


After struggling with os.spawnxxx to get a detached process I tried using 
Pyhton2.4's new subprocess module.

I struggled with that as well even when trying to use the creation flags for 
DETACHED_PROCESS 0x8 and CREATE_NEW_PROCESS_GROUP = 0x200

I am using the following cgi script parent.cgi

#!c:/python24/python.exe -u
print 'content-type: text/plain'
print
print 'DONE'
import subprocess
pid = subprocess.Popen(['c:/python24/pythonw.exe',
     'c:\\tmp\\child.py', 'arg1', 'arg2'], creationflags=0x208).pid
print pid
#end of parent script


child is a simple time delay

#child.py
import time, sys, os
time.sleep(10)
print 'stdout IN THE CHILD'
print >>sys.stderr, 'stderr IN THE CHILD'
#end of child


When I run the parent.cgi script through apache I see no output in the browser 
for 10 seconds; when the child dies (as observed using procexp) I see the expected

DONE
3256

In other words it seems impossible to get standard subprocess.py to detach the 
child process properly.

However, if I hack subprocess.py to alter the bInheritHandles flag passed into 
CreateProcess (line 718) from the constant 1 to

not (creationflags & 0x8) and 1 or 0

Is this a buglet or a feature request? It seems subprocess punts on closefds for 
mswindows, but setting bInheritHandles to 0 seems to work fine ie when it is 0 
my test seems to indicate that the parent has finished and gone to heaven long 
before the child ends its sleep. Of course it may be that it's just the handles 
that are being held.
-- 
Robin Becker




More information about the Python-list mailing list