class WorkerPool(object): def __init__(self, num_threads=None): if num_threads is None: num_threads = multiprocessing.cpu_count() self.tasks = Queue(num_threads) for _ in range(num_threads): Worker(self.tasks) def add_task(self, func, *args, **kargs): self.tasks.put((func, args, kargs)) def wait_for_completion(self): self.tasks.join()
def __init__(self, num_threads=None): if num_threads is None: num_threads = multiprocessing.cpu_count() self.tasks = Queue(num_threads) for _ in range(num_threads): Worker(self.tasks)