Advertisement
rfmonk

queue_priority.py

Jan 13th, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. import Queue
  5. import threading
  6.  
  7.  
  8. class Job(object):
  9.     def __init__(self, priority, description):
  10.         self.priority = priority
  11.         self.description = description
  12.         print 'New job:', description
  13.         return
  14.  
  15.     def __cmp__(self, other):
  16.         return cmp(self.priority, other.priority)
  17.  
  18. q = Queue.PriorityQueue()
  19.  
  20. q.put(Job(3, 'Mid-level job'))
  21. q.put(Job(10, 'Low-level job'))
  22. q.put(Job(1, 'Important job'))
  23.  
  24.  
  25. def process_job(q):
  26.     while True:
  27.         next_job = q.get()
  28.         print 'Processing job:', next_job.description
  29.         q.task_done()
  30.  
  31. workers = [threading.Thread(target=process_job, args=(q,)),
  32.            threading.Thread(target=process_job, args=(q,)),
  33.            ]
  34.  
  35. for w in workers:
  36.     w.setDaemon(True)
  37.     w.start()
  38.  
  39. q.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement