How to detach Python child process on Windows (without setsid)?

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.

1 answer

  • answered 2018-09-21 19:40 Neil C. Obremski

    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.