blob: 8543c5a6a47dd8a456eb2396dd833cd0bacc386a [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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.
Mark Summerfieldfcb444a2007-09-04 08:16:15 +000010It is especially useful in threaded programming when information must be
Georg Brandl8ec7f652007-08-15 14:28:01 +000011exchanged safely between multiple threads. The :class:`Queue` class in this
12module implements all the required locking semantics. It depends on the
Mark Summerfieldfcb444a2007-09-04 08:16:15 +000013availability of thread support in Python; see the :mod:`threading`
14module.
Georg Brandl8ec7f652007-08-15 14:28:01 +000015
16The :mod:`Queue` module defines the following class and exception:
17
18
19.. class:: Queue(maxsize)
20
21 Constructor for the class. *maxsize* is an integer that sets the upperbound
22 limit on the number of items that can be placed in the queue. Insertion will
23 block once this size has been reached, until queue items are consumed. If
24 *maxsize* is less than or equal to zero, the queue size is infinite.
25
26
27.. exception:: Empty
28
29 Exception raised when non-blocking :meth:`get` (or :meth:`get_nowait`) is called
30 on a :class:`Queue` object which is empty.
31
32
33.. exception:: Full
34
35 Exception raised when non-blocking :meth:`put` (or :meth:`put_nowait`) is called
36 on a :class:`Queue` object which is full.
37
38
39.. _queueobjects:
40
41Queue Objects
42-------------
43
44Class :class:`Queue` implements queue objects and has the methods described
45below. This class can be derived from in order to implement other queue
46organizations (e.g. stack) but the inheritable interface is not described here.
47See the source code for details. The public methods are:
48
49
50.. method:: Queue.qsize()
51
Raymond Hettinger907cda62008-01-15 05:46:43 +000052 Return the approximate size of the queue. Note, qsize() > 0 doesn't
53 guarantee that a subsequent get() will not block, nor will qsize() < maxsize
Skip Montanarof233b0c2008-01-15 03:40:20 +000054 guarantee that put() will not block.
Georg Brandl8ec7f652007-08-15 14:28:01 +000055
56
57.. method:: Queue.empty()
58
Skip Montanarof233b0c2008-01-15 03:40:20 +000059 Return ``True`` if the queue is empty, ``False`` otherwise. If empty()
60 returns ``True`` it doesn't guarantee that a subsequent call to put()
61 will not block. Similarly, if empty() returns ``False`` it doesn't
62 guarantee that a subsequent call to get() will not block.
Georg Brandl8ec7f652007-08-15 14:28:01 +000063
64
65.. method:: Queue.full()
66
Skip Montanarof233b0c2008-01-15 03:40:20 +000067 Return ``True`` if the queue is full, ``False`` otherwise. If full()
68 returns ``True`` it doesn't guarantee that a subsequent call to get()
69 will not block. Similarly, if full() returns ``False`` it doesn't
70 guarantee that a subsequent call to put() will not block.
Georg Brandl8ec7f652007-08-15 14:28:01 +000071
72
73.. method:: Queue.put(item[, block[, timeout]])
74
75 Put *item* into the queue. If optional args *block* is true and *timeout* is
76 None (the default), block if necessary until a free slot is available. If
77 *timeout* is a positive number, it blocks at most *timeout* seconds and raises
78 the :exc:`Full` exception if no free slot was available within that time.
79 Otherwise (*block* is false), put an item on the queue if a free slot is
80 immediately available, else raise the :exc:`Full` exception (*timeout* is
81 ignored in that case).
82
83 .. versionadded:: 2.3
84 The *timeout* parameter.
85
86
87.. method:: Queue.put_nowait(item)
88
89 Equivalent to ``put(item, False)``.
90
91
92.. method:: Queue.get([block[, timeout]])
93
94 Remove and return an item from the queue. If optional args *block* is true and
95 *timeout* is None (the default), block if necessary until an item is available.
96 If *timeout* is a positive number, it blocks at most *timeout* seconds and
97 raises the :exc:`Empty` exception if no item was available within that time.
98 Otherwise (*block* is false), return an item if one is immediately available,
99 else raise the :exc:`Empty` exception (*timeout* is ignored in that case).
100
101 .. versionadded:: 2.3
102 The *timeout* parameter.
103
104
105.. method:: Queue.get_nowait()
106
107 Equivalent to ``get(False)``.
108
109Two methods are offered to support tracking whether enqueued tasks have been
110fully processed by daemon consumer threads.
111
112
113.. method:: Queue.task_done()
114
115 Indicate that a formerly enqueued task is complete. Used by queue consumer
116 threads. For each :meth:`get` used to fetch a task, a subsequent call to
117 :meth:`task_done` tells the queue that the processing on the task is complete.
118
119 If a :meth:`join` is currently blocking, it will resume when all items have been
120 processed (meaning that a :meth:`task_done` call was received for every item
121 that had been :meth:`put` into the queue).
122
123 Raises a :exc:`ValueError` if called more times than there were items placed in
124 the queue.
125
126 .. versionadded:: 2.5
127
128
129.. method:: Queue.join()
130
131 Blocks until all items in the queue have been gotten and processed.
132
133 The count of unfinished tasks goes up whenever an item is added to the queue.
134 The count goes down whenever a consumer thread calls :meth:`task_done` to
135 indicate that the item was retrieved and all work on it is complete. When the
136 count of unfinished tasks drops to zero, join() unblocks.
137
138 .. versionadded:: 2.5
139
140Example of how to wait for enqueued tasks to be completed::
141
142 def worker():
143 while True:
144 item = q.get()
145 do_work(item)
146 q.task_done()
147
148 q = Queue()
149 for i in range(num_worker_threads):
150 t = Thread(target=worker)
151 t.setDaemon(True)
152 t.start()
153
154 for item in source():
155 q.put(item)
156
157 q.join() # block until all tasks are done
158