Python it is possible to spawn background process which runs even if main process exits?
<p>What I'm trying to achieve:
I want that my main process spawn another independent python process. 
Like:</p>

<pre><code>for i in range(2):
    p = proces()
    p.start()

sys.exit(0)
</code></pre>

<p>And my spawned processes should run in the background doing some task :)
Because at the moment, if I spawn a process using <code>multiprocessing</code> library and then close my main scripts these processes also exits.</p>

<pre><code>import time

import multiprocessing

import sys


def run_forever():
    print "ENTERED SUBPROCESS"
    while True:
        print "HELLO from subprocess"
        time.sleep(2)
    print "GOODBYE SUBPROCESS"


if __name__ == "__main__":
    print "STARTING IN MAIN"
    multiprocessing.Process(target=run_forever).start()
    time.sleep(1)
    print "LEAVING MAIN PROG"
    sys.exit()
</code></pre>

<p>results in the following output</p>

<pre><code>STARTING IN MAIN
ENTERED SUBPROCESS
HELLO
LEAVING MAIN PROG
HELLO
HELLO
HELLO
HELLO
HELLO
...
</code></pre>

<p>of coarse you may have a new problem now (ie you dont actually want run_forever to run forever ... there is no way to exit this process now ...)</p>

python multithreading multiprocessing