Python Glossary Русский

Python 3.0+queueterm

queue

module

Thread-safe queues for passing data between threads: FIFO, LIFO and priority queues.

Syntax

import queue

queue is for exchanging data between threading threads: put and get do the locking themselves, and get without a timeout blocks until an item arrives. For a plain queue in one thread collections.deque is faster; for processes use multiprocessing.Queue, for asyncio use asyncio.Queue. get_nowait on an empty queue raises queue.Empty. In Python 2 the module was called Queue.

Examples

import queue, threading
q = queue.Queue()
threading.Thread(target=lambda: [q.put(i) for i in range(3)]).start()
print([q.get() for _ in range(3)])  # → [0, 1, 2]
q.get_nowait()  # → queue.Empty

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy