examining items in a python Queue

前端 未结 3 631

Is it possible to examine items in a Queue in Python without calling .get()? As per the docs, indexing is not allowed in Queue. I need to check if

3条回答
  •  半阙折子戏
    2020-12-15 21:21

    queue_object.queue will return copy of your queue in a deque object which you can then use the slices of. It is of course, not syncronized with the original queue, but will allow you to peek at the queue at the time of the copy.

    There's a good rationalization for why you wouldn't want to do this explained in detail in this thread comp.lang.python - Queue peek?. But if you're just trying to understand how Queue works, this is one simple way.

    import Queue
    q = Queue.Queue()
    q.push(1)
    q.put('foo')
    q.put('bar')
    d = q.queue
    print(d)
    deque(['foo', 'bar'])
    print(d[0])
    'foo'
    

提交回复
热议问题