Created
August 27, 2013 13:40
-
-
Save engineersamuel/6353669 to your computer and use it in GitHub Desktop.
Simple thread pool in python
This file contains 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
class OneOffThreadPool(threading.Thread): | |
def __init__(self, max_concurrent_threads=10): | |
threading.Thread.__init__(self) | |
self.threads = [] | |
self.threads_running = [] | |
self.thread_count = 0 | |
self.max_concurrent_threads = max_concurrent_threads | |
self.log = logging.getLogger(name="OneOffThreadPool") | |
self.log.setLevel(logging.INFO) | |
self.log.addHandler(console) | |
def add_thread(self, t): | |
self.threads.append(t) | |
def run(self): | |
if len(self.threads) > 0: | |
while True: | |
# TODO | |
# Same as below except after popping if the dependencies haven't been run yet, push back onto the list? | |
# While the current thread count is < than the defined concurrent thread # start a new thread | |
if (len(self.threads_running) <= self.max_concurrent_threads) and len(self.threads) > 0: | |
self.log.debug("Under concurrent limit, at %s, starting a new thread." % len(self.threads_running)) | |
t = self.threads.pop() | |
t.start() | |
self.threads_running.append(t) | |
# Otherwise there are more threads to consume, but we hit the concurrent limit | |
# So join 1 and continue the loop | |
else: | |
self.log.debug("At concurrent thread limit, joining and existing thread.") | |
t = self.threads_running.pop(0) | |
t.join() | |
self.log.info("\tDone {}, name: {}.".format(t.display, t.name)) | |
del t | |
# If there are no more threads to consume, join the running and exit. | |
if len(self.threads) == 0: | |
self.log.debug("No more threads to start, joining the existing threads.") | |
# join all threads and break | |
for t in self.threads_running: | |
t.join() | |
self.threads_running.remove(t) | |
del t | |
self.log.debug("Operation complete, breaking.") | |
break | |
# Release the GIL | |
sleep(.00001) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment