Python: clear items from PriorityQueue
Question: [Clear all items from the queue
](http://stackoverflow.com/questions/6517953/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())?


Answer: 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](http://stackoverflow.com/a/18873213/3165737):

    
    
    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.

