It looks like you want the child process to be daemonized - PEP 3143, which I point to, documents and points to the reference implementation for this, and also points to others.
As soon as your process (still running Python code) is demonized, be it using the tools offered in PEP 3143 or others, you can os.execl (or another function os.exec... ) your target code - this runs the specified target code exactly in the same process that we just said is demonized, and thus it continues to be demonized at will.
The last step cannot use subprocess , because it must run in the same (demonized) process, overlaying its executable code - exactly that for os.execl and for friends.
The first step before demonizing it would be to do this through subprocess , but it is somewhat inconvenient (you need to put the daemonize-then-os.exec code in a separate .py ): most often you just want os.fork and immediately os.fork child process.
subprocess pretty convenient as it is basically a cross-platform way to start other processes, but it cannot really replace the good old Unix fork and exec approach for extended use (like demonization) - thatβs why itβs good that the standard Python library also allows you to do the latter using these functions in the os module! -)
Alex martelli
source share