![]() ![]() ![]() If you inserted them in any other order, you'd still get them in order (1, 'job1'), (1, 'job2'), (1, 'job3'), (1, 'job4').Ībout your added example: Your Job objects only compare themselves by their priority. And you'll get (1, 'job2') second not because it was inserted second but because it's the second-smallest item. You'll get (1, 'job1') first not because it was inserted first but because it's the smallest of the four tuples (because the priorities are the same and 'job1' is the smallest of the four strings). Although that's because you're inserting them in sorted order and they only have one possible order, as there are no equal items. If you just get all four items of that example with q.get(), you'll see that it does give them to you in your insertion order. the order you should instead care about is the order you actually get. It's not that "the order has been mixed" as you put it, it's that you misinterpreted that list. But if you do inspect it, you need to look at it as the heap it is, not think of it as a sorted list. So when the queue asks such a tuple whether it's smaller than another, it's the tuple that looks into itself and considers the priority first and then potentially the task second.Īlso, with q.queue you're inspecting the queue's underlying data structure. Tuples are ordered lexicographically, so (a, b) is smaller than (c, d) if a is smaller than c or if they're equal and b is smaller than d. That's why, when you want to prioritize tasks not just by their natural order (e.g., the string 'job1' being smaller than the string 'job2'), you use a tuple of priority and task. For its prioritization it just asks the whole item whether it's smaller than another. So you could also give it task strings alone, without extra priorities. Only we the users think of the pair as priority and task. It just thinks of the pair as one "item" and it never even looks into it. It doesn't think of the two values as priority and task. Note that you give Python's priority queue pairs of priority and task, but the queue doesn't care. Fortunately it's as simple as storing entries "as 3-element list including the priority, an entry count, and the task". If you want stability, you'll need to enforce it yourself. That's also why heapsort "is not a stable sort". And heaps don't naturally offer stability. As the documentation says, it's "using the heapq module". Priority queues "are often implemented with heaps" and Python is no exception. The expected result: QUIT goes out first, and then the rest, FIFO ordered: Build, Clean, Create, Build, Clean: > q.get() Now I'll dequeue the elements one by one. Return cmp(self.priority, other.priority) Self.priority = 0 if self.type_ = 'QUIT' else 1 I was asked to add an example that shows that q.get() actually mess things up with the FIFO ordering, so here's an elaborate example: class Job(object): What's the reason? how to overcome (keep the order of same prio elements)? For some reason this is not the case: > from Queue import PriorityQueueĪs can be seen from the example, the order has been mixed after one get(). I'm using python's Queue.PriorityQueue, and ran into the following problem: when inserting several elements to the queue which have the same priority, I would expect the queue to serve them in the order of insertion (FIFO). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |