根据docs:
首先检索最低值的条目(最低值的条目
是 sorted(list(entries))[0]) 返回的那个。典型模式
for entries 是一个元组,格式为:(priority_number, data)。
更新:
您可以创建一个三元组以避免重复,如下所示:
>>> import queue
>>> class Foo(object):
... def __init__(self, bar):
... self.bar = bar
...
>>> f1 = Foo('foo')
>>> f2 = Foo('bar')
>>> f3 = Foo('baz')
>>> f4 = Foo('bar')
>>> q = queue.PriorityQueue()
>>> q.put((5,0,f1))
>>> q.put((3,0,f2))
>>> q.put((3,1,f3))
>>> q.put((1,0,f4))
或者(可能更简洁),实现magic methods:
>>> import queue
>>> q = queue.PriorityQueue()
>>> class Foo(object):
... def __init__(self, bar):
... self.bar = bar
... def __eq__(self, other):
... return self.bar == other.bar
... def __ne__(self, other):
... return self.bar != other.bar
... def __lt__(self, other):
... return self.bar < other.bar
... def __gt__(self, other):
... return self.bar > other.bar
... def __le__(self, other):
... return self.bar <= other.bar
... def __ge__(self, other):
... return self.bar >= other.bar
...
>>> f1 = Foo('foo')
>>> f2 = Foo('bar')
>>> f3 = Foo('baz')
>>> f4 = Foo('baa')
>>> f5 = Foo('baz')
>>> q.put(f1)
>>> q.put(f2)
>>> q.put(f3)
>>> q.put(f4)
>>> q.put(f5)