Skip to content

Instantly share code, notes, and snippets.

@phizaz
Last active June 15, 2016 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phizaz/82bb6b2f11ac16d9bb029bb3af21f256 to your computer and use it in GitHub Desktop.
Save phizaz/82bb6b2f11ac16d9bb029bb3af21f256 to your computer and use it in GitHub Desktop.
A single worker with queue and promise pattern (inspiration from Javascript)
from Queue import Queue
from threading import Thread
'''
A single worker with queue and promise pattern
usage: worker.add_job(fn).then(callback_fn)
note: this might be anti-pattern
'''
class Promise:
def __init__(self):
self.val = None
self.success = False
self.callback = None
def done(self, value):
# print('job done:', value)
self.val = value
if self.callback:
self.callback(value)
def then(self, callback):
self.callback = callback
def is_ready(self):
return self.success is True
class Worker:
def __init__(self, cnt=1):
self.queue = Queue()
self.threads = self._init_threads(cnt)
self.results = []
def add_job(self, fn):
promise = Promise()
self.queue.put((len(self.results), fn, promise))
self.results.append(None)
return promise
def close(self):
self.queue.join()
return self.results
def _worker(self):
while True:
i, fn, promise = self.queue.get()
# print('new job:', item)
r = fn()
self.results[i] = r
promise.done(r)
self.queue.task_done()
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment