Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create child processes inside a child process with Python multiprocessing failed

I observed this behavior when trying to create nested child processes in Python. Here is the parent program parent_process.py:

import multiprocessing
import child_process

pool = multiprocessing.Pool(processes=4)
for i in range(4):
        pool.apply_async(child_process.run, ())
pool.close()
pool.join()

The parent program calls the "run" function in the following child program child_process.py:

import multiprocessing

def run():
        pool = multiprocessing.Pool(processes=4)
        print 'TEST!'
        pool.close()
        pool.join()

When I run the parent program, nothing was printed out and the program exited quickly. However, if print 'TEST!' is moved one line above (before the nested child processes are created), 'TEST!' are printed for 4 times.

Because errors in a child process won't print to screen, this seems to show that the program crashes when a child process creates its own nested child processes.

Could anyone explain what happens behind the scene? Thanks!

like image 257
Da Kuang Avatar asked Sep 21 '15 06:09

Da Kuang


1 Answers

According to the documentation for multiprocessing, daemonic processes cannot spawn child processes.

multiprocessing.Pool uses daemonic processes to ensure that they don't leak when your program exits.

like image 89
noxdafox Avatar answered Sep 26 '22 02:09

noxdafox