When you execute a python script, does the process/interpreter exit because it reads an EOF character from the script? [i.e. is that the exit signal?]
The follow up to this is how/when a python child process knows to exit, namely, when you start a child process by overriding the run() method, as here:
class Example(multiprocessing.Process):
def __init__(self, task_queue, result_queue):
multiprocessing.Process.__init__(self)
self.task_queue = task_queue
self.result_queue = result_queue
def run(self):
while True:
next_task = self.task_queue.get()
if next_task is None:
print '%s: Exiting' % proc_name
break
#more stuff...[assume there's some task_done stuff, etc]
if __name__ == '__main__':
tasks = multiprocessing.JoinableQueue()
results = multiprocessing.Queue()
processes = [ Example(tasks, results)
for i in range(5) ]
for i in processes:
i.start()
#more stuff...like populating the queue, etc.
Now, what I’m curious about is: Do the child processes automatically exit upon completion of the run() method? And if I kill the main thread during execution, will the child processes end immediately? Will they end if their run() calls can complete independently of the status of the parent process?
Yes, each child process terminates automatically after completion of the
runmethod, even though I think you should avoid subclassingProcessand use thetargetargument instead.Note that in linux the child process may remain in zombie state if you do not read the exit status:
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
Processinstance 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:
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
initand are still running.But, as I said, this is OS specific. On some OSes killing the parent process will kill all child processes.