How Does A Python Process Know When To Exit?
Solution 1:
Yes, each child process terminates automatically after completion of the run
method, even though I think you should avoid subclassing Process
and use the target
argument instead.
Note that in linux the child process may remain in zombie state if you do not read the exit status:
>>>from multiprocessing import Process>>>deftarget():...print("Something")...>>>Process(target=target).start()>>>Something>>>
If we look at the processes after this:
While if we read the exit status of the process (with Process.exitcode
), this does not happen.
Each Process
instance launches a new process in the background, how and when this subprocess is terminated is OS-dependant. Every OS provides some mean of communication between processes. Child processes are usually not terminated if you kill the "parent" process.
For example doing this:
>>>from multiprocessing import Process>>>import time>>>deftarget():...whileTrue:... time.sleep(0.5)...>>>L = [Process(target=target) for i inrange(10)]>>>for p in L: p.start()...
The main python process will have 10 children:
Now if we kill that process we obtain this:
Note how the child processes where inherited by
init
and are still running.
But, as I said, this is OS specific. On some OSes killing the parent process will kill all child processes.
Post a Comment for "How Does A Python Process Know When To Exit?"