Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear all items from the queue

Tags:

python

queue

How can I clear a queue. For example I have datas in a queue, but for some reason I don't need the existing data, and just want to clear the queue.

Is there any way? Will this work:

oldQueue = Queue.Queue() 
like image 999
OHLÁLÁ Avatar asked Jun 29 '11 08:06

OHLÁLÁ


People also ask

How do I clear all elements in a queue?

When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque. Queue . Clear Method is used to remove the objects from the Queue . This method is an O(n) operation, where n is total count of elements.

How do you clear the queue in Python?

To clear all items from a queue in Python:Call the clear() method on the deque object, e.g. q. queue. clear() . The clear method will remove all elements from the queue.

How do I clear my multiprocessing queue?

Simply use q = ClearableQueue() in all places where you used q = Queue() , and call q. clear() when you'd like.

How do I find the size of a python queue?

To get the length of a queue in Python:Use the len() function to get the length of a deque object. Use the qsize() method to get the length of a queue object.


1 Answers

q = Queue.Queue() q.queue.clear() 

EDIT I omitted the issue of thread safety for clarity and brevity, but @Dan D is quite correct, the following is better.

q = Queue.Queue() with q.mutex:     q.queue.clear() 
like image 110
Rob Cowie Avatar answered Oct 06 '22 01:10

Rob Cowie