from queue import PriorityQueue
class Job(object):
def __init__(self, priority, description):
self.priority = priority
self.description = description
print('New job:', description)
return
# 优先级比较
def __lt__(self, other):
return self.priority < other.priority
q = PriorityQueue()
#q.put((数字,值)),特点:数字越小,优先级越高
q.put(Job(5, 'Mid-level job'))
q.put(Job(10, 'Low-level job'))
q.put(Job(1, 'Important job'))
while not q.empty():
next_job = q.get()
print('Processing job', next_job.description)
New job: Mid-level job
New job: Low-level job
New job: Important job
Processing job Important job
Processing job Mid-level job
Processing job Low-level job