rest25/library/queue.rst => rest262/library/queue.rst
n1- 
2-:mod:`Queue` --- A synchronized queue class
1+:mod:`queue` --- A synchronized queue class
3===========================================
4
5.. module:: Queue
6   :synopsis: A synchronized queue class.
7
n7+.. 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.
8
n12+ 
9-The :mod:`Queue` module implements a multi-producer, multi-consumer FIFO queue.
13+The :mod:`Queue` module implements multi-producer, multi-consumer queues.
10-It is especially useful in threads programming when information must be
14+It is especially useful in threaded 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
n13-availability of thread support in Python.
n17+availability of thread support in Python; see the :mod:`threading`
18+module.
14
n15-The :mod:`Queue` module defines the following class and exception:
n20+Implements three types of queue whose only difference is the order that
21+the entries are retrieved.  In a FIFO queue, the first tasks added are
22+the first retrieved. In a LIFO queue, the most recently added entry is
23+the first retrieved (operating like a stack).  With a priority queue,
24+the entries are kept sorted (using the :mod:`heapq` module) and the
25+lowest valued entry is retrieved first.
16
n27+The :mod:`Queue` module defines the following classes and exceptions:
17
18.. class:: Queue(maxsize)
19
n20-   Constructor for the class.  *maxsize* is an integer that sets the upperbound
n31+   Constructor for a FIFO queue.  *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
n36+.. 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
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
n69+.. 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+ 
37
38.. _queueobjects:
39
40Queue Objects
41-------------
42
n43-Class :class:`Queue` implements queue objects and has the methods described
n81+Queue objects (:class:`Queue`, :class:`LifoQueue`, or :class:`PriorityQueue`)
44-below.  This class can be derived from in order to implement other queue
82+provide the public methods described below.
45-organizations (e.g. stack) but the inheritable interface is not described here.
46-See the source code for details.  The public methods are:
47
48
n49-.. method:: XXX Class.qsize()
n85+.. method:: Queue.qsize()
50
n51-   Return the approximate size of the queue.  Because of multithreading semantics,
n87+   Return the approximate size of the queue.  Note, qsize() > 0 doesn't
52-   this number is not reliable.
88+   guarantee that a subsequent get() will not block, nor will qsize() < maxsize
89+   guarantee that put() will not block.
53
54
n55-.. method:: XXX Class.empty()
n92+.. method:: Queue.empty()
56
n57-   Return ``True`` if the queue is empty, ``False`` otherwise. Because of
n94+   Return ``True`` if the queue is empty, ``False`` otherwise.  If empty()
58-   multithreading semantics, this is not reliable.
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.
59
60
n61-.. method:: XXX Class.full()
n100+.. method:: Queue.full()
62
n63-   Return ``True`` if the queue is full, ``False`` otherwise. Because of
n102+   Return ``True`` if the queue is full, ``False`` otherwise.  If full()
64-   multithreading semantics, this is not reliable.
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.
65
66
n67-.. method:: XXX Class.put(item[, block[, timeout]])
n108+.. 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
77   .. versionadded:: 2.3
n78-      the timeout parameter.
n119+      The *timeout* parameter.
79
80
n81-.. method:: XXX Class.put_nowait(item)
n122+.. method:: Queue.put_nowait(item)
82
83   Equivalent to ``put(item, False)``.
84
85
n86-.. method:: XXX Class.get([block[, timeout]])
n127+.. method:: Queue.get([block[, timeout]])
87
88   Remove and return an item from the queue. If optional args *block* is true and
89   *timeout* is None (the default), block if necessary until an item is available.
90   If *timeout* is a positive number, it blocks at most *timeout* seconds and
91   raises the :exc:`Empty` exception if no item was available within that time.
92   Otherwise (*block* is false), return an item if one is immediately available,
93   else raise the :exc:`Empty` exception (*timeout* is ignored in that case).
94
95   .. versionadded:: 2.3
n96-      the timeout parameter.
n137+      The *timeout* parameter.
97
98
n99-.. method:: XXX Class.get_nowait()
n140+.. method:: Queue.get_nowait()
100
101   Equivalent to ``get(False)``.
102
103Two methods are offered to support tracking whether enqueued tasks have been
104fully processed by daemon consumer threads.
105
106
n107-.. method:: XXX Class.task_done()
n148+.. method:: Queue.task_done()
108
109   Indicate that a formerly enqueued task is complete.  Used by queue consumer
110   threads.  For each :meth:`get` used to fetch a task, a subsequent call to
111   :meth:`task_done` tells the queue that the processing on the task is complete.
112
113   If a :meth:`join` is currently blocking, it will resume when all items have been
114   processed (meaning that a :meth:`task_done` call was received for every item
115   that had been :meth:`put` into the queue).
116
117   Raises a :exc:`ValueError` if called more times than there were items placed in
118   the queue.
119
120   .. versionadded:: 2.5
121
122
n123-.. method:: XXX Class.join()
n164+.. method:: Queue.join()
124
125   Blocks until all items in the queue have been gotten and processed.
126
127   The count of unfinished tasks goes up whenever an item is added to the queue.
128   The count goes down whenever a consumer thread calls :meth:`task_done` to
129   indicate that the item was retrieved and all work on it is complete. When the
n130-   count of unfinished tasks drops to zero, join() unblocks.
n171+   count of unfinished tasks drops to zero, :meth:`join` unblocks.
131
132   .. versionadded:: 2.5
133
134Example of how to wait for enqueued tasks to be completed::
135
n136-   def worker(): 
n177+   def worker():
137-       while True: 
178+       while True:
138-           item = q.get() 
179+           item = q.get()
139-           do_work(item) 
180+           do_work(item)
140-           q.task_done() 
181+           q.task_done()
141
n142-   q = Queue() 
n183+   q = Queue()
143-   for i in range(num_worker_threads): 
184+   for i in range(num_worker_threads):
144        t = Thread(target=worker)
145        t.setDaemon(True)
n146-        t.start() 
n187+        t.start()
147
148   for item in source():
t149-       q.put(item) 
t190+       q.put(item)
150
151   q.join()       # block until all tasks are done
152
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op