Skip to content

Instantly share code, notes, and snippets.

@rjosephwright
Last active December 20, 2015 02:59
Show Gist options
  • Select an option

  • Save rjosephwright/6060201 to your computer and use it in GitHub Desktop.

Select an option

Save rjosephwright/6060201 to your computer and use it in GitHub Desktop.
_dispatch_queue = Queue()
_queues = {}
def get_queue(qname):
return _queues[qname]
def dispatcher():
while True:
task = _dispatch_queue.get()
if not task: break # "shutdown" happens by putting None on the queue, so it will break here
if task.qname not in _queues:
_queues[task.qname] = Queue()
t = Thread(target=task.executor, args=(task.qname,))
t.daemon = True
t.start()
_queues[task.qname].put(task)
class Task(object):
def __init__(self, qname):
self.qname = qname
self.returnq = Queue(1)
def run(self):
_dispatch_queue.put(self)
return self
def get(self):
return self.returnq.get()
def put(self, item):
self.returnq.put(item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment