Would it be possible to create a python Pool that is non-daemonic? I want a pool to be able to call a function that has another pool inside.
I want this because deamon processes cannot create process. Specifically, it will cause the error:
AssertionError: daemonic processes are not allowed to have children
For example, consider the scenario where function_a has a pool which runs function_b which has a pool which runs function_c. This function chain will fail, because function_b is being run in a daemon process, and daemon processes cannot create processes.
The
multiprocessing.pool.Poolclass creates the worker processes in its__init__method, makes them daemonic and starts them, and it is not possible to re-set theirdaemonattribute toFalsebefore they are started (and afterwards it’s not allowed anymore). But you can create your own sub-class ofmultiprocesing.pool.Pool(multiprocessing.Poolis just a wrapper function) and substitute your ownmultiprocessing.Processsub-class, which is always non-daemonic, to be used for the worker processes.Here’s a full example of how to do this. The important parts are the two classes
NoDaemonProcessandMyPoolat the top and to callpool.close()andpool.join()on yourMyPoolinstance at the end.