class ClusterPool: def __init__(self): self.manager = Manager() self.clusters = self.manager.list() def __enter__(self): return self def __exit__(self, exc_type, exc_value, tb): self.manager.__exit__(exc_type, exc_value, tb)
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))