This should make it easier for you to use the Pool . As for speed, starting processes takes time. However, using Pool , unlike starting njobs Process should be as fast as you can get it working with processes. The default value for Pool (as used below) is to use the maximum number of available processes (i.e. the number of CPUs you use) and continue working with new jobs for the worker as soon as the task is completed. You will not get njobs -way in parallel, but you will get so much parallelism that your processors can process without re-subscribing your processors. I use pathos , which has a multiprocessing fork, because it is a little more reliable than standard multiprocessing ... and, well, I'm also an author. But you could use multiprocessing for this.
>>> from pathos.multiprocessing import ProcessingPool as Pool >>> class A(object): ... def __init__(self, njobs=1000): ... self.map = Pool().map ... self.njobs = njobs ... self.start() ... def start(self): ... self.result = self.map(self.RunProcess, range(self.njobs)) ... return self.result ... def RunProcess(self, i): ... return i*i ... >>> myA = A() >>> myA.result[:11] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100] >>> myA.njobs = 3 >>> myA.start() [0, 1, 4]
This is a bit of a strange construct to run Pool inside __init__ . But if you want to do this, you need to get results from something like self.result ... and you can use self.start for subsequent calls.
Get pathos here: https://github.com/uqfoundation
source share