Last active
December 20, 2015 02:59
-
-
Save rjosephwright/6060201 to your computer and use it in GitHub Desktop.
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
| _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