• 4
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I want to get the next item in queue but I don't want to dequeue it. Is it possible in Python's queue.PriorityQueue? From the docs, I don't see how can it be done

If a is a PriorityQueue object, You can use a.queue[0] to get the next item:

from Queue import PriorityQueue

a = PriorityQueue()

a.put((10, "a"))
a.put((4, "b"))
a.put((3,"c"))

print a.queue
print a.get()
print a.queue
print a.get()
print a.queue

output is :

[(3, 'c'), (10, 'a'), (4, 'b')]
(3, 'c')
[(4, 'b'), (10, 'a')]
(4, 'b')
[(10, 'a')]

but be careful about multi thread access.

  • 37
Reply Report
    • In the case of multi threading, we could lock the q.mutex, and release the lock after reading q.queue[0].
    • It appears that while q.queue[0] returns the highest priority item in the queue, q.queue[1] does not necessarily return the 2nd highest priority item
    • @Woofas What you say is totally true and from my point of view, it's an unexpected behavior... Do you know why it happens?
      • 1
    • @Woofas you will find that the 2nd highest priority is either q.queue[1] or q.queue[2]. That is because according to the theory of Priority Queues, the parent (q.queue[0] in this case) must have a higher priority that either of its two children (q.queue[1] and q.queue[2]), but the specific order of these two children is not important. This means that the whole q.queue is not absolutely sorted, only "heap" sorted (i.e every level has a higher priority than the level below it)

If you want next element in the PriorityQueue, in the order of the insertion of the elements, use:

for i in range(len(queue)):
    print queue.queue[i]

this will not pop anything out.

If you want it in the priority order, use:

for i in range(len(queue)):
    temp = queue.get()
    queue.put(temp)
    print temp

If you are using a tuple, instead of a single variable, replace temp by:

((temp1,temp2))
  • 5
Reply Report
      • 2
    • This solution isn't limited to just PriorityQueue objects. It also works for Queue objects. Seems like the most elegant solution to me. No offense intended, but I don't see how the other answers come close to this one (imho).
      • 1
    • The first part does not give you the elements in the inserted order, but the first element will be the one with lowest value.

Indexing the first element of the queue should work. If you're using the heapq library, the document mentions:

The interesting property of a heap is that its smallest element is always the root, heap[0].

  • 2
Reply Report

When you get item form the queue as per theory it will remove from the queue. You have to write your own function which will give you last element of PriorityQueue. You can create a peek function by inherit the priorityqueue.

  • 1
Reply Report

Trending Tags