How to let the child process live when parent process exited?
Asked Answered
S

2

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!!

Strickman answered 11/7, 2014 at 9:42 Comment(14)
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 docs.python.org/2/library/…Candler
Why would you let your child processes live without a parent? With no parent they will become zombies.Centigrade
@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.Gollin
@Candler Yes, you are right... I just found this in my test... The task in child do not do...Strickman
@Centigrade As you know, every process have a father process, As @Frédéric Hamidi said, father process exited, child process' father become init process.Strickman
Did not think of that, thank you both for clarifying!Centigrade
@Candler do you have some solution??Strickman
Sounds like you want to use something like nohup or os.fork. A similar question is here: #6011735Depressant
@Depressant Greet thanks~ Let me have a look~Strickman
@Depressant I just read the link, does it mean multiprocessing or subprocess can not do this? But, linux os programming is a perfect solution.Strickman
A trick: call os._exit to make parent process exit, in this way daemonic child processes will not be killed.Candler
@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.Thermocline
@k9x See this answer for a cross-platform way of properly launching a de-coupled child process using subprocess: https://mcmap.net/q/25390/-popen-waiting-for-child-process-even-when-the-immediate-child-has-terminatedThermocline
@Thermocline Big thanks, subprocess.Popen() did NOT block the father process, father process just exited, let child finish. U should answer this~Strickman
C
8

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.

Candler answered 11/7, 2014 at 10:38 Comment(1)
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 exitPitchford
V
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?

Voltmer answered 11/7, 2019 at 5:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.