-
-
Save ri0day/21e32e99949040685952 to your computer and use it in GitHub Desktop.
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
from threading import Thread | |
import Queue | |
import time | |
class TaskQueue(Queue.Queue): | |
def __init__(self, num_workers=1): | |
Queue.Queue.__init__(self) | |
self.num_workers = num_workers | |
self.start_workers() | |
def add_task(self, task, *args, **kwargs): | |
args = args or () | |
kwargs = kwargs or {} | |
self.put((task, args, kwargs)) | |
def start_workers(self): | |
for i in range(self.num_workers): | |
t = Thread(target=self.worker) | |
t.daemon = True | |
t.start() | |
def worker(self): | |
while True: | |
tupl = self.get() | |
item, args, kwargs = self.get() | |
item(*args, **kwargs) | |
self.task_done() | |
def tests(): | |
def blokkah(*args, **kwargs): | |
time.sleep(5) | |
print "Blokkah mofo!" | |
q = TaskQueue(num_workers=5) | |
for item in range(10): | |
q.add_task(blokkah) | |
q.join() # block until all tasks are done | |
print "All done!" | |
if __name__ == "__main__": | |
tests() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment