PageRenderTime 265ms CodeModel.GetById 140ms app.highlight 8ms RepoModel.GetById 113ms app.codeStats 0ms

/Doc/library/queue.rst

http://unladen-swallow.googlecode.com/
ReStructuredText | 193 lines | 126 code | 67 blank | 0 comment | 0 complexity | f57e6c1116a494e755833ab2627122c1 MD5 | raw file
  1:mod:`queue` --- A synchronized queue class
  2===========================================
  3
  4.. module:: Queue
  5   :synopsis: A synchronized queue class.
  6
  7.. note::
  8   The :mod:`Queue` module has been renamed to :mod:`queue` in Python 3.0.  The
  9   :term:`2to3` tool will automatically adapt imports when converting your
 10   sources to 3.0.
 11
 12
 13The :mod:`Queue` module implements multi-producer, multi-consumer queues.
 14It is especially useful in threaded programming when information must be
 15exchanged safely between multiple threads.  The :class:`Queue` class in this
 16module implements all the required locking semantics.  It depends on the
 17availability of thread support in Python; see the :mod:`threading`
 18module.
 19
 20Implements three types of queue whose only difference is the order that
 21the entries are retrieved.  In a FIFO queue, the first tasks added are
 22the first retrieved. In a LIFO queue, the most recently added entry is
 23the first retrieved (operating like a stack).  With a priority queue,
 24the entries are kept sorted (using the :mod:`heapq` module) and the
 25lowest valued entry is retrieved first.
 26
 27The :mod:`Queue` module defines the following classes and exceptions:
 28
 29.. class:: Queue(maxsize)
 30
 31   Constructor for a FIFO queue.  *maxsize* is an integer that sets the upperbound
 32   limit on the number of items that can be placed in the queue.  Insertion will
 33   block once this size has been reached, until queue items are consumed.  If
 34   *maxsize* is less than or equal to zero, the queue size is infinite.
 35
 36.. class:: LifoQueue(maxsize)
 37
 38   Constructor for a LIFO queue.  *maxsize* is an integer that sets the upperbound
 39   limit on the number of items that can be placed in the queue.  Insertion will
 40   block once this size has been reached, until queue items are consumed.  If
 41   *maxsize* is less than or equal to zero, the queue size is infinite.
 42
 43   .. versionadded:: 2.6
 44
 45.. class:: PriorityQueue(maxsize)
 46
 47   Constructor for a priority queue.  *maxsize* is an integer that sets the upperbound
 48   limit on the number of items that can be placed in the queue.  Insertion will
 49   block once this size has been reached, until queue items are consumed.  If
 50   *maxsize* is less than or equal to zero, the queue size is infinite.
 51
 52   The lowest valued entries are retrieved first (the lowest valued entry is the
 53   one returned by ``sorted(list(entries))[0]``).  A typical pattern for entries
 54   is a tuple in the form: ``(priority_number, data)``.
 55
 56   .. versionadded:: 2.6
 57
 58.. exception:: Empty
 59
 60   Exception raised when non-blocking :meth:`get` (or :meth:`get_nowait`) is called
 61   on a :class:`Queue` object which is empty.
 62
 63
 64.. exception:: Full
 65
 66   Exception raised when non-blocking :meth:`put` (or :meth:`put_nowait`) is called
 67   on a :class:`Queue` object which is full.
 68
 69.. seealso::
 70
 71   :class:`collections.deque` is an alternative implementation of unbounded
 72   queues with fast atomic :func:`append` and :func:`popleft` operations that
 73   do not require locking.
 74
 75
 76.. _queueobjects:
 77
 78Queue Objects
 79-------------
 80
 81Queue objects (:class:`Queue`, :class:`LifoQueue`, or :class:`PriorityQueue`)
 82provide the public methods described below.
 83
 84
 85.. method:: Queue.qsize()
 86
 87   Return the approximate size of the queue.  Note, qsize() > 0 doesn't
 88   guarantee that a subsequent get() will not block, nor will qsize() < maxsize
 89   guarantee that put() will not block.
 90
 91
 92.. method:: Queue.empty()
 93
 94   Return ``True`` if the queue is empty, ``False`` otherwise.  If empty()
 95   returns ``True`` it doesn't guarantee that a subsequent call to put()
 96   will not block.  Similarly, if empty() returns ``False`` it doesn't
 97   guarantee that a subsequent call to get() will not block.
 98
 99
100.. method:: Queue.full()
101
102   Return ``True`` if the queue is full, ``False`` otherwise.  If full()
103   returns ``True`` it doesn't guarantee that a subsequent call to get()
104   will not block.  Similarly, if full() returns ``False`` it doesn't
105   guarantee that a subsequent call to put() will not block.
106
107
108.. method:: Queue.put(item[, block[, timeout]])
109
110   Put *item* into the queue. If optional args *block* is true and *timeout* is
111   None (the default), block if necessary until a free slot is available. If
112   *timeout* is a positive number, it blocks at most *timeout* seconds and raises
113   the :exc:`Full` exception if no free slot was available within that time.
114   Otherwise (*block* is false), put an item on the queue if a free slot is
115   immediately available, else raise the :exc:`Full` exception (*timeout* is
116   ignored in that case).
117
118   .. versionadded:: 2.3
119      The *timeout* parameter.
120
121
122.. method:: Queue.put_nowait(item)
123
124   Equivalent to ``put(item, False)``.
125
126
127.. method:: Queue.get([block[, timeout]])
128
129   Remove and return an item from the queue. If optional args *block* is true and
130   *timeout* is None (the default), block if necessary until an item is available.
131   If *timeout* is a positive number, it blocks at most *timeout* seconds and
132   raises the :exc:`Empty` exception if no item was available within that time.
133   Otherwise (*block* is false), return an item if one is immediately available,
134   else raise the :exc:`Empty` exception (*timeout* is ignored in that case).
135
136   .. versionadded:: 2.3
137      The *timeout* parameter.
138
139
140.. method:: Queue.get_nowait()
141
142   Equivalent to ``get(False)``.
143
144Two methods are offered to support tracking whether enqueued tasks have been
145fully processed by daemon consumer threads.
146
147
148.. method:: Queue.task_done()
149
150   Indicate that a formerly enqueued task is complete.  Used by queue consumer
151   threads.  For each :meth:`get` used to fetch a task, a subsequent call to
152   :meth:`task_done` tells the queue that the processing on the task is complete.
153
154   If a :meth:`join` is currently blocking, it will resume when all items have been
155   processed (meaning that a :meth:`task_done` call was received for every item
156   that had been :meth:`put` into the queue).
157
158   Raises a :exc:`ValueError` if called more times than there were items placed in
159   the queue.
160
161   .. versionadded:: 2.5
162
163
164.. method:: Queue.join()
165
166   Blocks until all items in the queue have been gotten and processed.
167
168   The count of unfinished tasks goes up whenever an item is added to the queue.
169   The count goes down whenever a consumer thread calls :meth:`task_done` to
170   indicate that the item was retrieved and all work on it is complete. When the
171   count of unfinished tasks drops to zero, :meth:`join` unblocks.
172
173   .. versionadded:: 2.5
174
175Example of how to wait for enqueued tasks to be completed::
176
177   def worker():
178       while True:
179           item = q.get()
180           do_work(item)
181           q.task_done()
182
183   q = Queue()
184   for i in range(num_worker_threads):
185        t = Thread(target=worker)
186        t.setDaemon(True)
187        t.start()
188
189   for item in source():
190       q.put(item)
191
192   q.join()       # block until all tasks are done
193