Python Glossary Русский

Python 3.13+queueexception

queue.ShutDown

exceptions

The exception raised by put() and get() after shutdown() has been called on the queue (Python 3.13+).

Syntax

q.shutdown(immediate=False); except queue.ShutDown: ...

After shutdown(), any new put() fails with ShutDown at once, while get() still returns the remaining items and fails only when the queue is empty. With immediate=True the queue is drained and get() fails immediately. It is the standard way to stop consumer threads instead of a None sentinel.

Examples

import queue
q = queue.Queue()
q.put(1)
q.shutdown()
print(q.get())  # → 1 — остаток ещё отдаётся
try:
    q.get()
except queue.ShutDown:
    print('ShutDown')  # → ShutDown

See also

Open in the full glossarydocs.python.orgReport an inaccuracy