I'm migrating some process code to Windows which worked well on Posix. Very simply put: the code to launch a subprocess and immediately detach will not work because setsid()
is not available:
import os, subprocess, sys
p = subprocess.Popen([sys.executable, '-c', "print 'hello'"], preexec_fn=os.setsid)
I can remove the use of setsid
but then the child process ends when the parent ends.
My question is how do I achieve the same effect as setsid
on Windows so that the child process lifetime is independent of the parent's?
I'd be willing to use a particular Python package if one exists for this sort of thing. I'm already using psutil
for example but I didn't see anything in it that could help me.
RbMn's comment on my question is in fact the answer. There is no need to detach because processes in Windows are always top-level objects. I tried this out with a sleeper task and it worked fine.
However as eryksun pointed out in the comments here, closing the console window WILL cause the child process to terminate. I also had some stability issues with Waitress and Popen on Windows that I think I've worked through by adding the following bit of code which sets some process creation flags and uses close_fds
:
if 'nt' == os.name:
flags = 0
flags |= 0x00000008 # DETACHED_PROCESS
flags |= 0x00000200 # CREATE_NEW_PROCESS_GROUP
flags |= 0x08000000 # CREATE_NO_WINDOW
pkwargs = {
'close_fds': True, # close stdin/stdout/stderr on child
'creationflags': flags,
}
p = subprocess.Popen([sys.executable, '-c', cmd], **pkwargs)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.