Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate Queue.Queue items in Python?

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?

like image 486
pgilmon Avatar asked Nov 19 '11 18:11

pgilmon


People also ask

Can you iterate through a queue?

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++

What does queue () do in Python?

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.

How do I see queue items in Python?

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.


2 Answers

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 
like image 166
Raymond Hettinger Avatar answered Sep 19 '22 18:09

Raymond Hettinger


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] 
like image 34
Omer Dagan Avatar answered Sep 23 '22 18:09

Omer Dagan