10

I want to use multiprocessing module to complete this.

when I do this, like:

    $ python my_process.py

I start a parent process, and then let the parent process spawn a child process,

then i want that the parent process exits itself, but the child process continues to work.

Allow me write a WRONG code to explain myself:

from multiprocessing import Process

def f(x):
    with open('out.dat', 'w') as f:
        f.write(x)

if __name__ == '__main__':
    p = Process(target=f, args=('bbb',))
    p.daemon = True    # This is key, set the daemon, then parent exits itself
    p.start()

    #p.join()    # This is WRONG code, just want to exlain what I mean.
    # the child processes will be killed, when father exit

So, how do i start a process that will not be killed when the parent process finishes?


20140714

Hi, you guys

My friend just told me a solution...

I just think...

Anyway, just let u see:

import os
os.system('python your_app.py&')    # SEE!? the & !!

this does work!!

kkkkkk
  • 632
  • 2
  • 8
  • 14
  • 2
    No, a daemonic process will be killed when it's parent process exit. "When a process exits, it attempts to terminate all of its daemonic child processes." from https://docs.python.org/2/library/multiprocessing.html#multiprocessing.Process.daemon – WKPlus Jul 11 '14 at 09:58
  • Why would you let your child processes live without a parent? With no parent they will become zombies. – Pphoenix Jul 11 '14 at 09:59
  • 2
    @Pphoenix, not really. Zombies are child processes that have exited but whose parents still haven't waited for. Orphaned processes are adopted by `init` and keep going on. – Frédéric Hamidi Jul 11 '14 at 10:01
  • @WKPlus Yes, you are right... I just found this in my test... The task in child do not do... – kkkkkk Jul 11 '14 at 10:04
  • @Pphoenix As you know, every process have a father process, As @Frédéric Hamidi said, father process exited, child process' father become `init` process. – kkkkkk Jul 11 '14 at 10:06
  • Did not think of that, thank you both for clarifying! – Pphoenix Jul 11 '14 at 10:08
  • @WKPlus do you have some solution?? – kkkkkk Jul 11 '14 at 10:12
  • 2
    Sounds like you want to use something like `nohup` or `os.fork`. A similar question is here: http://stackoverflow.com/questions/6011235/run-a-program-from-python-and-have-it-continue-to-run-after-the-script-is-kille – Dunes Jul 11 '14 at 10:26
  • @Dunes Greet thanks~ Let me have a look~ – kkkkkk Jul 11 '14 at 10:29
  • @Dunes I just read the link, does it mean `multiprocessing` or `subprocess` can not do this? But, linux os programming is a perfect solution. – kkkkkk Jul 11 '14 at 10:33
  • A trick: call `os._exit` to make parent process exit, in this way daemonic child processes will not be killed. – WKPlus Jul 11 '14 at 10:36
  • 1
    @k9x You're really not supposed to use the `multiprocessing` module like this. It's meant to be used as a drop-in replacement for threads, which of course cannot live beyond the life of the parent process. If you want to spawn a process that can live beyond the life of the parent, use the `subprocess` module. – dano Jul 11 '14 at 14:32
  • 3
    @k9x See this answer for a cross-platform way of properly launching a de-coupled child process using `subprocess`: http://stackoverflow.com/a/13256908/2073595 – dano Jul 11 '14 at 14:48
  • @dano Big thanks, `subprocess.Popen()` did NOT block the father process, father process just exited, let child finish. U should answer this~ – kkkkkk Jul 14 '14 at 07:05

2 Answers2

7

A trick: call os._exit to make parent process exit, in this way daemonic child processes will not be killed.

But there are some other side affects, described in the doc:

Exit the process with status n, without calling cleanup handlers, 
flushing stdio buffers, etc.

If you do not care about this, you can use it.

WKPlus
  • 6,955
  • 2
  • 35
  • 53
  • what if you double fork? so parent spawns child1 that spawns your daemon. then child1 calls os._exit, and parent is free to do a clean exit – radu.ciorba Jul 11 '14 at 14:26
0

Here's one way to achieve an independent child process that does not exit when __main__ exits. It uses the os._exit() tip mentioned above by @WKPlus.

Is there a way to detach matplotlib plots so that the computation can continue?

Marc Compere
  • 290
  • 4
  • 17