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()
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.
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.
Simply use q = ClearableQueue() in all places where you used q = Queue() , and call q. clear() when you'd like.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With