def __init__(self, num_threads, num_users, sched_policy='default', queue_policy='single_task'): self.logger = logging.getLogger('ThreadPool') self.procs = [] self.tasks = [] self.num_user = num_users self.users = range(num_users) self.die = Value('b', False) self.scheduler = Scheduler(sched_policy) self.logger.info('%s policy choosen for scheduling', sched_policy) self.logger.info('%s policy choosen for queue', queue_policy) for _ in self.users: q = UsersQueue(0, queue_policy) self.tasks.append(q) self.scheduler.register_user(_, q) for _ in range(num_threads): p = Worker(self.tasks, self.die, self.scheduler) self.procs.append(p)
class ThreadPool: """Pool of threads consuming tasks from a queue""" def __init__(self, num_threads, num_users, sched_policy='default', queue_policy='single_task'): self.logger = logging.getLogger('ThreadPool') self.procs = [] self.tasks = [] self.num_user = num_users self.users = range(num_users) self.die = Value('b', False) self.scheduler = Scheduler(sched_policy) self.logger.info('%s policy choosen for scheduling', sched_policy) self.logger.info('%s policy choosen for queue', queue_policy) for _ in self.users: q = UsersQueue(0, queue_policy) self.tasks.append(q) self.scheduler.register_user(_, q) for _ in range(num_threads): p = Worker(self.tasks, self.die, self.scheduler) self.procs.append(p) def kill(self): self.die.value = True def add_task(self, num, func, *args, **kargs): """Add a task to the queue""" """Because MAX_NUMBER_JOBS_IN_QUEUE is aproximate, I dont use lock here""" if not self.die.value and self.tasks[num].active_tasks.value < MAX_NUMBER_JOBS_IN_QUEUE: self.tasks[num].q.put((func, args, kargs)) self.tasks[num].active_tasks.value += 1 else: raise Exception('Unable to add task') def wait_completion(self): """Wait for completion of all the tasks in the queue""" [p.join() for p in self.procs]