Home>

In the queue moduleq.get ()Is it possible to see the contents without taking out the contents of the queue?
I looked it up but it didn't come out ...

It seems that you can see it using the collections module, but I'm wondering, so I'll ask you a question.

import collections
q = collections.deque ()
q.append ("aaa")
q.append ("bbb")
print (q)
# deque (['aaa','bbb'])
#Reference: https://python.atelierkobato.com/queue/
  • Answer # 1

    You can see it in the queue object's instance variable queue.

    However, it is not documented because I want to hide it intentionally.
    Do not operate on the internal .queue directly from the outside.
    In some cases, it will not function as a queue.

    q = Queue ()
    #DO NOT !!
    q.queue.append (1)
    # OK
    q.put (1)
    #Check the contents
    with q.mutex:
        print (q.queue)

    * When reading using a thread
    Note that the execution result may differ depending on the timing.

    For debugging purposes, use the logging module
    It's better to log get/put to the queue.

    At the time of display, there was an item in the queue,
    It can happen that it wasn't the actual timing, so
    Not recommended as a debugging method (when threads are involved).