python multiprocessing: why is process defunct after terminate?

6.9k views Asked by At

I have some python multiprocessing code with the parent process starting a bunch of child worker processes and then terminating them after awhile:

from multiprocessing import Process

nWorkers = 10
curWorkers = []
for iw in range(nWorkers):
    pq = Process(target=worker, args=(worker's_args_here))
    pq.start()
    curWorkers.append(pq)

# Do work here...

for pw in curWorkers:
    pw.terminate()

However, the child processes all are showing as defunct long after termination. Are they zombie processes? More importantly, how should I terminate them so that they really go away?

1

There are 1 answers

0
Tim Peters On BEST ANSWER

Try adding:

for pw in curWorkers:
    pw.join()

at the end. .terminate() just kills the process. The parent process still needs to reap it (at least on Linux-y systems) before the child process goes away entirely.