Ejemplo n.º 1
0
class MolPool(object):
    def __init__(self, calc, nproc):
        self.pool = Pool(nproc)
        self.mgr = Manager()
        self.calc = calc
        self.calc_proxy = self.mgr.list([calc])
        self.nproc = nproc

    def __enter__(self):
        self.mgr.__enter__()
        return self

    def __exit__(self, *args, **kwargs):
        self.pool.terminate()
        self.mgr.__exit__(*args, **kwargs)

    def map(self, mols, id):
        return MolIterator(self, mols, id, self.nproc * 2 + 10)

    def submit(self, mol, id):
        cxt = Context.from_calculator(self.calc, mol, id)
        return self.pool.apply_async(worker, (self.calc_proxy, cxt))