Python Glossary Русский

all Python 3queueterm

queue.LifoQueue

queues

A thread-safe LIFO queue (a stack): get() returns the most recently added item.

Syntax

q = queue.LifoQueue(maxsize=0)

The interface matches Queue (put, get, task_done, join); only the order of retrieval differs. For a stack in a single thread a plain list with append/pop is enough, and LifoQueue's locking only slows it down. Use it when several threads share a stack of tasks, such as a depth-first traversal.

Examples

import queue
q = queue.LifoQueue()
for x in 'abc':
    q.put(x)
print(q.get(), q.get(), q.get())  # → c b a

See also

Open in the full glossarydocs.python.orgReport an inaccuracy