Does anyone know a pythonic way of iterating over the elements of a Queue.Queue
without removing them from the Queue. I have a producer/consumer-type program where items to be processed are passed by using a Queue.Queue
, and I want to be able to print what the remaining items are. Any ideas?
We can iterate on queue using std: :front which will return the front element of the queue and std: :pop which will remove the front element of the queue. But, after iterating on the queue with this method queue will vanish as we are deleting elements of the queue as we iterate over it. Example: C++
Queue in Python is a linear data structure with a rear and a front end, similar to a stack. It stores items sequentially in a FIFO (First In First Out) manner. You can think of it as a customer services queue that functions on a first-come-first-serve basis.
To check if an element is in a queue in Python: Access the queue attribute on the queue to get a deque object. Use the in operator to check if the element is in the queue. The in operator tests for membership.
You can loop over a copy of the underlying data store:
for elem in list(q.queue)
Eventhough this bypasses the locks for Queue objects, the list copy is an atomic operation and it should work out fine.
If you want to keep the locks, why not pull all the tasks out of the queue, make your list copy, and then put them back.
mycopy = [] while True: try: elem = q.get(block=False) except Empty: break else: mycopy.append(elem) for elem in mycopy: q.put(elem) for elem in mycopy: # do something with the elements
Listing queue elements without consuming them:
>>> from Queue import Queue >>> q = Queue() >>> q.put(1) >>> q.put(2) >>> q.put(3) >>> print list(q.queue) [1, 2, 3]
After operation, you can still process them:
>>> q.get() 1 >>> print list(q.queue) [2, 3]
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