Если я хочу распечатать значения в PriorityQueue
, как я могу это сделать?Я расширил PriortiyQueue
и получил доступ к базовому списку queue
.Однако когда я делаю цикл for, я получаю порядок, в котором добавляются элементы
# the ADT PriorityQueue
class ReadyQueue(PriorityQueue):
...
def listAll(self):
print("PID \t Name \t Status \t Priority")
print("=" * 42)
for _, _, pcb in self.queue:
print(pcb)
# ADT
class PCB:
...
def __lt__(self, other):
selfPriority = (self.priority, self.creationTime)
otherPriority = (other.priority, other.creationTime)
return selfPriority < otherPriority
# in the main function
q = ReadyQueue()
q.enqueue(PCB("p1", 0, None, q))
q.enqueue(PCB("p8", 6, None, q))
q.enqueue(PCB("p2", 1, None, q))
q.enqueue(PCB("p0", 1, None, q))
q.enqueue(PCB("p6", 1, None, q))
q.enqueue(PCB("p4", 1, None, q))
q.enqueue(PCB("p3", 2, None, q))
q.listAll()
Вывод:
PID Name Status Priority
==========================================
0 p1 ready_s 0
3 p0 ready_s 1
2 p2 ready_s 1
1 p8 ready_s 6
4 p6 ready_s 1
5 p4 ready_s 1
6 p3 ready_s 2
Обратите внимание, что приоритет не верен.Хотя они и не появляются в порядке создания ... я что-то пропустил?