我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用multiprocessing.BoundedSemaphore()。
def __init__(self, log_name, experiments, cpu_limit=2**16): """ :param experiments: A list of pypuf.experiments.experiment.base.Experiment :param log_name: A unique file path where to output should be logged. :param cpu_limit: Maximum number of parallel processes that run experiments. """ # Store experiments list self.experiments = experiments # Store logger name self.logger_name = log_name # Setup parallel execution limit self.cpu_limit = min(cpu_limit, multiprocessing.cpu_count()) self.semaphore = multiprocessing.BoundedSemaphore(self.cpu_limit)
def main_threaded(iniconfig): semaphore = BoundedSemaphore(CONCURRENCY_LIMIT) tasks = [] for appid in iniconfig: section = iniconfig[appid] task = Thread(target=checker, args=(section, appid, semaphore)) tasks.append(task) task.start() try: for t in tasks: t.join() except KeyboardInterrupt: for t in tasks: if hasattr(t, 'terminate'): # multiprocessing t.terminate() print 'Validation aborted.' sys.exit(1)
def __init__(self): # if any jobs marked in run state when scheduler starts # replace their state with X to mark that they have been shutdown db = DAL(config.uri, auto_import=True, migrate=False, folder=config.dbdir) myset = db(db.jobs.state == STATE_RUN) myset.update(state=STATE_STOPPED) db.commit() self.sem = BoundedSemaphore(config.np) self.mutex = Lock() # set time zone try: os.environ['TZ'] = config.time_zone time.tzset() except: pass
def test_bounded_semaphore(self): sem = self.BoundedSemaphore(2) self._test_semaphore(sem) # Currently fails on OS/X #if HAVE_GETVALUE: # self.assertRaises(ValueError, sem.release) # self.assertReturnsIfImplemented(2, get_value, sem)