Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Queue import Queue
- from threading import Thread
- class Worker(Thread):
- def __init__(self, tasks):
- Thread.__init__(self)
- self.tasks = tasks
- self.daemon = True
- self.start()
- def run(self):
- while True:
- func, args, kargs = self.tasks.get()
- try: func(*args, **kargs)
- except Exception, e: print e
- self.tasks.task_done()
- class ThreadPool:
- def __init__(self, num_threads):
- 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_completion(self):
- self.tasks.join()
- pool = ThreadPool(25)
- pool.add_task(function, value)
- pool.wait_completion()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement