blob: c183860331f92c3dc622ddb136e86ee94da75302 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`Queue` --- A synchronized queue class
3===========================================
4
5.. module:: Queue
6 :synopsis: A synchronized queue class.
7
8
9The :mod:`Queue` module implements a multi-producer, multi-consumer FIFO queue.
10It is especially useful in threads programming when information must be
11exchanged safely between multiple threads. The :class:`Queue` class in this
12module implements all the required locking semantics. It depends on the
13availability of thread support in Python.
14
15The :mod:`Queue` module defines the following class and exception:
16
17
18.. class:: Queue(maxsize)
19
20 Constructor for the class. *maxsize* is an integer that sets the upperbound
21 limit on the number of items that can be placed in the queue. Insertion will
22 block once this size has been reached, until queue items are consumed. If
23 *maxsize* is less than or equal to zero, the queue size is infinite.
24
25
26.. exception:: Empty
27
28 Exception raised when non-blocking :meth:`get` (or :meth:`get_nowait`) is called
29 on a :class:`Queue` object which is empty.
30
31
32.. exception:: Full
33
34 Exception raised when non-blocking :meth:`put` (or :meth:`put_nowait`) is called
35 on a :class:`Queue` object which is full.
36
37
38.. _queueobjects:
39
40Queue Objects
41-------------
42
43Class :class:`Queue` implements queue objects and has the methods described
44below. This class can be derived from in order to implement other queue
45organizations (e.g. stack) but the inheritable interface is not described here.
46See the source code for details. The public methods are:
47
48
49.. method:: Queue.qsize()
50
51 Return the approximate size of the queue. Because of multithreading semantics,
52 this number is not reliable.
53
54
55.. method:: Queue.empty()
56
57 Return ``True`` if the queue is empty, ``False`` otherwise. Because of
58 multithreading semantics, this is not reliable.
59
60
61.. method:: Queue.full()
62
63 Return ``True`` if the queue is full, ``False`` otherwise. Because of
64 multithreading semantics, this is not reliable.
65
66
67.. method:: Queue.put(item[, block[, timeout]])
68
69 Put *item* into the queue. If optional args *block* is true and *timeout* is
70 None (the default), block if necessary until a free slot is available. If
71 *timeout* is a positive number, it blocks at most *timeout* seconds and raises
72 the :exc:`Full` exception if no free slot was available within that time.
73 Otherwise (*block* is false), put an item on the queue if a free slot is
74 immediately available, else raise the :exc:`Full` exception (*timeout* is
75 ignored in that case).
76
Georg Brandl116aa622007-08-15 14:28:22 +000077
78.. method:: Queue.put_nowait(item)
79
80 Equivalent to ``put(item, False)``.
81
82
83.. method:: Queue.get([block[, timeout]])
84
85 Remove and return an item from the queue. If optional args *block* is true and
86 *timeout* is None (the default), block if necessary until an item is available.
87 If *timeout* is a positive number, it blocks at most *timeout* seconds and
88 raises the :exc:`Empty` exception if no item was available within that time.
89 Otherwise (*block* is false), return an item if one is immediately available,
90 else raise the :exc:`Empty` exception (*timeout* is ignored in that case).
91
Georg Brandl116aa622007-08-15 14:28:22 +000092
93.. method:: Queue.get_nowait()
94
95 Equivalent to ``get(False)``.
96
97Two methods are offered to support tracking whether enqueued tasks have been
98fully processed by daemon consumer threads.
99
100
101.. method:: Queue.task_done()
102
103 Indicate that a formerly enqueued task is complete. Used by queue consumer
104 threads. For each :meth:`get` used to fetch a task, a subsequent call to
105 :meth:`task_done` tells the queue that the processing on the task is complete.
106
107 If a :meth:`join` is currently blocking, it will resume when all items have been
108 processed (meaning that a :meth:`task_done` call was received for every item
109 that had been :meth:`put` into the queue).
110
111 Raises a :exc:`ValueError` if called more times than there were items placed in
112 the queue.
113
Georg Brandl116aa622007-08-15 14:28:22 +0000114
115.. method:: Queue.join()
116
117 Blocks until all items in the queue have been gotten and processed.
118
119 The count of unfinished tasks goes up whenever an item is added to the queue.
120 The count goes down whenever a consumer thread calls :meth:`task_done` to
121 indicate that the item was retrieved and all work on it is complete. When the
122 count of unfinished tasks drops to zero, join() unblocks.
123
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125Example of how to wait for enqueued tasks to be completed::
126
127 def worker():
128 while True:
129 item = q.get()
130 do_work(item)
131 q.task_done()
132
133 q = Queue()
134 for i in range(num_worker_threads):
135 t = Thread(target=worker)
136 t.setDaemon(True)
137 t.start()
138
139 for item in source():
140 q.put(item)
141
142 q.join() # block until all tasks are done
143