Created
April 29, 2013 17:52
-
-
Save cjhanks/5483383 to your computer and use it in GitHub Desktop.
Minimal ThreadPool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from threading import Thread | |
| from Queue import Queue | |
| class ThreadPool(object): | |
| # Nested private worker class | |
| class Worker(Thread): | |
| def __init__(self, queue): | |
| Thread.__init__(self) | |
| self.queue = queue | |
| def run(self): | |
| while True: | |
| runnable = self.queue.get() | |
| # on None terminate thread | |
| if None == runnable: | |
| break | |
| else: | |
| runnable() | |
| self.queue.task_done() | |
| def __init__(self, count = 4): | |
| """ | |
| Initialize a ThreadPool with a count of N workers. | |
| """ | |
| self.__queue = Queue() | |
| self.__count = count | |
| for _ in range(count): | |
| ThreadPool.Worker(self.__queue).start() | |
| def __del__(self): | |
| """ | |
| Signal a None termination signal for every worker thread started. | |
| """ | |
| for _ in range(self.__count): | |
| self.__queue.put(None) | |
| def add_job(self, functor): | |
| self.__queue.put(functor) | |
| def join(self): | |
| return self.__queue.join() | |
| class ExampleRunnable(object): | |
| """ | |
| An example class which is packed with data and executed | |
| """ | |
| def __init__(self, some_var): | |
| self.some_var = some_var | |
| def __call__(self): | |
| import time | |
| print(self.some_var) | |
| time.sleep(2) | |
| def thread_pool_test(): | |
| tp = ThreadPool() | |
| for i in range(0, 12): | |
| tp.add_job(ExampleRunnable(i)) | |
| tp.join() | |
| thread_pool_test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment