Skip to content

Instantly share code, notes, and snippets.

@phizaz
Last active June 15, 2016 15:21
Show Gist options
  • Save phizaz/050fcf3ee24500ef4f1b6c95633b4aeb to your computer and use it in GitHub Desktop.
Save phizaz/050fcf3ee24500ef4f1b6c95633b4aeb to your computer and use it in GitHub Desktop.
EasyThreading: an alternative to Python's ThreadPool, but this version can make use of any kind of functions
from threading import Thread
from Queue import Queue
'''
an alternative to Python's ThreadPool, but this version can make use of any kind of functions
'''
class ThreadPool:
def __init__(self, cnt):
self.queue = Queue()
self.threads = self._init_threads(cnt)
self.results = None
def map(self, fn, iter):
things = list(iter)
# allocate
self.results = [None for i in things]
for i, arg in enumerate(things):
self.queue.put((i, fn, arg))
# wait
self.queue.join()
return self.results
def close(self):
self.results = None
def _init_threads(self, cnt):
threads = []
for i in range(cnt):
thread = Thread(target=self._worker)
threads.append(thread)
thread.daemon = True
thread.start()
return threads
def _worker(self):
while True:
i, fn, arg = self.queue.get()
self.results[i] = fn(arg)
self.queue.task_done()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment