4

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?

4

1 回答 1

2

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
>>> def target():
...     print("Something")
... 
>>> Process(target=target).start()
>>> Something

>>> 

If we look at the processes after this:

enter image description here

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
>>> def target():
...     while True:
...             time.sleep(0.5)
... 
>>> L = [Process(target=target) for i in range(10)]
>>> for p in L: p.start()
... 

The main python process will have 10 children:

enter image description here

Now if we kill that process we obtain this:

enter image description here 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.

于 2013-01-20T11:20:30.590 回答