Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Queue import PriorityQueue
- from threading import Thread
- from time import sleep
- from random import randrange
- import multiprocessing
- class MyPriorityQueue(PriorityQueue):
- def __init__(self):
- PriorityQueue.__init__(self)
- self.counter = 0
- def put(self, item, priority):
- PriorityQueue.put(self, (priority, self.counter, item))
- self.counter += 1
- def get(self, *args, **kwargs):
- _, _, item = PriorityQueue.get(self, *args, **kwargs)
- return item
- class Worker(Thread):
- """Thread executing tasks from a given tasks queue"""
- def __init__(self, tasks):
- Thread.__init__(self)
- self.tasks = tasks
- self.daemon = True
- self.start()
- def run(self):
- while True:
- # self.tasks = PriorityQueue
- # .get -> function, arg, kargs
- # dosmth(interval, foo=bar, zeugs=toll)
- func, args, kargs = self.tasks.get()
- try:
- func(*args, **kargs)
- except Exception, e:
- print e
- finally:
- self.tasks.task_done()
- class ThreadPool:
- """Pool of threads consuming tasks from a queue"""
- def __init__(self, num_threads):
- self.tasks = MyPriorityQueue()
- for _ in range(num_threads): Worker(self.tasks)
- def add_task(self, priority, func, *args, **kargs):
- """Add a task to the queue"""
- self.tasks.put((func, args, kargs), priority)
- def wait_completion(self):
- """Wait for completion of all the tasks in the queue"""
- self.tasks.join()
- def waitAndSleep(time,i):
- if i % 20 == 0:
- pool.add_task(-10, waitAndSleep, d, i+101)
- print 'sleeping for (%d)sec at %d' % (time,i)
- sleep(time)
- if __name__ == '__main__':
- delays = [randrange(1, 4) for i in range(100)]
- cpus = multiprocessing.cpu_count()
- print cpus
- pool = ThreadPool(cpus)
- while True:
- task = fetchtaskFromBoinc()
- pool.add_task(0,task)
- for i, d in enumerate(delays):
- pool.add_task(0, waitAndSleep, d, i)
- pool.wait_completion()%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement