我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用multiprocessing.pool.terminate()。
def list_all_machines(cloud_ids, headers): "Given the cloud ids, runs in parallel queries to get all machines" def list_one_cloud(cloud_id): cloud_machines = requests.get('https://mist.io/clouds/%s/machines' % cloud_id, headers=headers) if cloud_machines.status_code == 200: machines = cloud_machines.json() for machine in machines: machine['cloud'] = cloud_id return machines return [] pool = multiprocessing.pool.ThreadPool(8) results = pool.map(list_one_cloud, cloud_ids) pool.terminate() machines = [] for result in results: machines.extend(result) return machines
def closing_pool(pool): try: with contextlib.closing(pool) as pll: yield pll except Exception as exc: syslog.syslog(syslog.LOG_WARNING, "Terminate pool due to {0}".format(exc)) pool.terminate() raise finally: pool.join()
def ScopedPool(*args, **kwargs): """Context Manager which returns a multiprocessing.pool instance which correctly deals with thrown exceptions. *args - Arguments to multiprocessing.pool Kwargs: kind ('threads', 'procs') - The type of underlying coprocess to use. **etc - Arguments to multiprocessing.pool """ if kwargs.pop('kind', None) == 'threads': pool = multiprocessing.pool.ThreadPool(*args, **kwargs) else: orig, orig_args = kwargs.get('initializer'), kwargs.get('initargs', ()) kwargs['initializer'] = _ScopedPool_initer kwargs['initargs'] = orig, orig_args pool = multiprocessing.pool.Pool(*args, **kwargs) try: yield pool pool.close() except: pool.terminate() raise finally: pool.join()