12

If I am using the python module queue.Queue, I want to be able to print out the contents using a method that does not pop the original queue or create a new queue object.

I have tried looking into doing a get and then putting the contents back but this is too high cost.

# Ideally it would look like the following
from queue import Queue
q = Queue()
q.print()
q.put(1)
q.print()

>> [] # Or something like this
>> [1] # Or something like this
Interlooper
  • 494
  • 2
  • 5
  • 14

5 Answers5

26
>>> print(list(q.queue))

Does this work for you?

Aviral Verma
  • 488
  • 4
  • 11
3

Assuming you are using python 2. You can use something like this:

from queue import Queue
q = Queue.Queue()
q.put(1)
q.put(2)
q.put(3)
print q.queue

You can also loop on it :

for q_item in q.queue:
    print q_item

But unless you are dealing with threads, I would use a normal list as a Queue implementation.

255.tar.xz
  • 700
  • 7
  • 23
Paulo Cirino
  • 994
  • 7
  • 9
  • Thanks for the comment, in this particular scenario, I am using threads. The iteration through the lists seems interesting and useful maybe to print a specific matching element. – Interlooper Feb 12 '19 at 18:40
2

Sorry, I am a bit late to answer this question, but going by this comment, I extended the Queue in the multiprocessing package as per your requirements. Hopefully it will help someone in the future.

import multiprocessing as mp
from multiprocessing import queues


class IterQueue(queues.Queue):

    def __init__(self, *args, **kwargs):
        ctx = mp.get_context()
        kwargs['ctx'] = ctx
        super().__init__(*args, **kwargs)

    # <----  Iter Protocol  ------>
    def __iter__(self):
        return self

    def __next__(self):
        try:
            if not self.empty():
                return self.get()  # block=True | default
            else:
                raise StopIteration
        except ValueError:  # the Queue is closed
            raise StopIteration

Given below is a sample usage of this IterQueue I wrote:

def sample_func(queue_ref):
    for i in range(10):
        queue_ref.put(i)


IQ = IterQueue()

p = mp.Process(target=sample_func, args=(IQ,))
p.start()
p.join()

print(list(IQ))  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

I have tested this IterQueue for even a few more complex scenarios, and it seems to be working fine. Let me know if you think this works, or it could fail in some situation.

Archit
  • 976
  • 16
  • 28
0

not sure if this is still a question, but using the name.queue (i.e. q.queue here) works for me. This works also for the other types of queues in the module.

import queue

q = queue.Queue() 

print(list(q.queue))
q.put(1)
print(list(q.queue))
-1

The most common way to print the content of the queue if you are not using the queue then use this code snippet:

class Queue:

    def __init__(self):
     self.items = []

 
    def push(self, e):
      self.items.append(e)
 
    def pop(self):
      head = self.items[0]
      self.items = self.item[1:]
      return head

    def print(self):
      for e in self.items:
          print(e)
q = Queue()
q.push(1)
q.push(23)
q.print()

OUTPUT

1
23
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44
  • The OP was using the standard `queue.Queue` but your answer is not, and following comments indicated it was about the multiprocessing queue specifically. – Lenormju Jun 08 '21 at 11:41