How to iterate Queue.Queue items in Python?

前端 未结 3 1889
傲寒
傲寒 2020-12-28 12:40

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 p

3条回答
  •  星月不相逢
    2020-12-28 12:54

    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]
    

提交回复
热议问题