I'm using the python multiprocessing module to run some long running tasks in parallel. I'm using the start() method to run the job, but once the jobs have returned I'd like to run them again.
Is it possible to reuse the Process I create? or do I have to create a new Process object every time I want to run the job?
There is this section from the pyhton docs suggesting I cannot use the start() method more than onece, but perhaps someone knows of another way to reuse the instance:
start()
Start the process’s activity.
This must be called at most once per process object. It arranges for the object’s run() method to be invoked in a separate process.
This is my version of the Process class:
class Process(multiprocessing.Process):
def __init__(self, result_queue, MCMCinstance):
assert isinstance(MCMCinstance, MCMC)
multiprocessing.Process.__init__(self)
self.result_queue = result_queue
self.mcmc = MCMCinstance
self.interface = C_interface(self.mcmc)
self.burn_in = False
def run(self):
if self.burn_in: interface.burn_in()
self.interface.sample(self.mcmc.options.runs)
self.interface.update(self.mcmc)
self.result_queue.put(self.mcmc)
Then I instantiate the Processes and run them using the start() method:
# setup the jobs and run
result_queue = multiprocessing.Queue()
mcmc1 = MCMC(options, donors, clusters)
mcmc2 = MCMC(options, donors, clusters)
mcmc3 = MCMC(options, donors, clusters)
mcmc4 = MCMC(options, donors, clusters)
p1 = Process(result_queue, mcmc1)
p2 = Process(result_queue, mcmc2)
p3 = Process(result_queue, mcmc3)
p4 = Process(result_queue, mcmc4)
jobs = [p1, p2, p3, p4]
for job in jobs:
job.start()
results = [result_queue.get() for job in jobs]