How can I implement non-daemonic processes with pathos in python3 instead of with the multiprocessing module?
To be more specific, I am referring to:
Python Process Pool non-daemonic?
The answer to this post implements non-daemonic processes via the multiprocessing module. Unfortunately, this module does not allow to pickle lambda functions among other objects, but pathos does in Python 2:
#import multiprocessing
#import multiprocessing.pool
import pathos
#class NoDaemonProcess(multiprocessing.Process):
class NoDaemonProcess(pathos.multiprocessing.Pool.Process):
def _get_daemon(self):
return False
def _set_daemon(self, value):
pass
daemon = property(_get_daemon, _set_daemon)
#class NoDaemonPool(multiprocessing.pool.Pool):
class NoDaemonPool(pathos.multiprocessing.Pool):
Process = NoDaemonProcess
def myproc(args):
i, max_workers = args
#pool = multiprocessing.Pool(max_workers)
pool = pathos.pools.ProcessPool(max_workers)
l_args = [j for j in range(i)]
mysubproc = lambda x : x
print("myproc", l_args, pool.map(mysubproc, l_args))
return i
max_workers = [2, 1]
executor = NoDaemonPool(max_workers[0])
#executor = pathos.multiprocessing.Pool(max_workers[0])
l_args = [(i, max_workers[1]) for i in range(10)]
print(executor.map(myproc, l_args))
output:
('myproc', [], [])
('myproc', [0, 1], [0, 1])
('myproc', [0], [0])
('myproc', [0, 1, 2], [0, 1, 2])
('myproc', [0, 1, 2, 3], [0, 1, 2, 3])
('myproc', [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5])
('myproc', [0, 1, 2, 3, 4], [0, 1, 2, 3, 4])
('myproc', [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6])
('myproc', [0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7])
('myproc', [0, 1, 2, 3, 4, 5, 6, 7, 8], [0, 1, 2, 3, 4, 5, 6, 7, 8])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In Python 3, the pathos module has changed w.r.t. Python 2, e.g.,
pathos.multiprocessing.Pool.Process
is not a class anymore, but a function, so one can no longer use it for inheritance (see above).
- any pathos documentation I am missing?
How can I make the above code work in pathos in Python 3?
As a work-around for the above particular example, one can simply fall back to the multiprocessing NoDaemonPool implementation, and use pathos for the daemon sub-processes:
import multiprocessing
import multiprocessing.pool
import pathos
class NoDaemonProcess(multiprocessing.Process):
#class NoDaemonProcess(pathos.multiprocessing.Pool.Process):
def _get_daemon(self):
return False
def _set_daemon(self, value):
pass
daemon = property(_get_daemon, _set_daemon)
class NoDaemonPool(multiprocessing.pool.Pool):
#class NoDaemonPool(pathos.multiprocessing.Pool):
Process = NoDaemonProcess
def myproc(args):
i, max_workers = args
#pool = multiprocessing.Pool(max_workers)
pool = pathos.pools.ProcessPool(max_workers)
l_args = [j for j in range(i)]
mysubproc = lambda x : x
print("myproc", l_args, pool.map(mysubproc, l_args))
return i
max_workers = [2, 1]
executor = NoDaemonPool(max_workers[0])
#executor = pathos.multiprocessing.Pool(max_workers[0])
l_args = [(i, max_workers[1]) for i in range(10)]
print(executor.map(myproc, l_args))
However, this work-around is not a solution because
(i) its imports both pathos and multiprocessing, and even more importantly
(ii) it won't be able to pickle, e.g., if myproc instead is defined as
myproc = lambda x : x
Thank you very much,
Best,
Sebastian
See Question&Answers more detail:
os