Clear all items from the queue
I read the above answer
Im using python 2.7
import Queue
pq = Queue.PriorityQueue()
pq.clear()
I get the following error:
AttributeError: PriorityQueue instance has no attribute 'clear'
Is there way to easily empty priority queue instead of manually popping out all items? or would re-instantiating work (i.e. it wouldn't mess with join())?
It's actually pq.queue.clear()
. However, as mentioned in the answers to the question you referenced, this is not documented and potentially unsafe.
The cleanest way is described in this answer:
while not q.empty():
try:
q.get(False)
except Empty:
continue
q.task_done()
Re-instantiating the queue would work too of course (the object would simple be removed from memory), as long as no other part of your code holds on to a reference to the old queue.