使用NPE的策略-一个tuple作为队列优先级,tuple是(fpriority, spriority):import Queue
class Job(object):
def __init__(self, fpriority, spriority, description='blah', iata='foo' , hops='ample', cost='free pitchers'):
self.fpriority = fpriority
self.spriority = spriority
self.description = description
@property
def priority(self):
return (self.fpriority, self.spriority)
def __str__(self):
return self.description
q = Queue.PriorityQueue()
second = Job(2, 5, 'Mid-level job')
third = Job(2, 20, 'Low-level job')
first = Job(1, 20, 'Important job')
q.put((second.priority, second))
q.put((third.priority, third))
q.put((first.priority, first))
while q.unfinished_tasks:
task = q.get()
print task, task[1]
q.task_done()
>>>
((1, 20), <__main__.job object at>) Important job
((2, 5), <__main__.job object at>) Mid-level job
((2, 20), <__main__.job object at>) Low-level job
>>>
这对优先级元组中的任意数量的项都有效。在
^{pr2}$