blob: 4b9a1ebc2422c4b0e2b829d7076c4b37b5ca5f52 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
Alexandre Vassalottif260e442008-05-11 19:59:59 +00002:mod:`queue` --- A synchronized queue class
Georg Brandl116aa622007-08-15 14:28:22 +00003===========================================
4
Alexandre Vassalottif260e442008-05-11 19:59:59 +00005.. module:: queue
Georg Brandl116aa622007-08-15 14:28:22 +00006 :synopsis: A synchronized queue class.
7
8
Alexandre Vassalottif260e442008-05-11 19:59:59 +00009The :mod:`queue` module implements multi-producer, multi-consumer queues.
Thomas Wouters89d996e2007-09-08 17:39:28 +000010It is especially useful in threaded programming when information must be
Georg Brandl116aa622007-08-15 14:28:22 +000011exchanged safely between multiple threads. The :class:`Queue` class in this
12module implements all the required locking semantics. It depends on the
Thomas Wouters89d996e2007-09-08 17:39:28 +000013availability of thread support in Python; see the :mod:`threading`
14module.
Georg Brandl116aa622007-08-15 14:28:22 +000015
Raymond Hettinger35641462008-01-17 00:13:27 +000016Implements three types of queue whose only difference is the order that
17the entries are retrieved. In a FIFO queue, the first tasks added are
18the first retrieved. In a LIFO queue, the most recently added entry is
19the first retrieved (operating like a stack). With a priority queue,
20the entries are kept sorted (using the :mod:`heapq` module) and the
21lowest valued entry is retrieved first.
Georg Brandl116aa622007-08-15 14:28:22 +000022
Alexandre Vassalottif260e442008-05-11 19:59:59 +000023The :mod:`queue` module defines the following classes and exceptions:
Georg Brandl116aa622007-08-15 14:28:22 +000024
25.. class:: Queue(maxsize)
26
Raymond Hettinger35641462008-01-17 00:13:27 +000027 Constructor for a FIFO queue. *maxsize* is an integer that sets the upperbound
Georg Brandl116aa622007-08-15 14:28:22 +000028 limit on the number of items that can be placed in the queue. Insertion will
29 block once this size has been reached, until queue items are consumed. If
30 *maxsize* is less than or equal to zero, the queue size is infinite.
31
Christian Heimes679db4a2008-01-18 09:56:22 +000032
Raymond Hettinger35641462008-01-17 00:13:27 +000033.. class:: LifoQueue(maxsize)
34
35 Constructor for a LIFO queue. *maxsize* is an integer that sets the upperbound
36 limit on the number of items that can be placed in the queue. Insertion will
37 block once this size has been reached, until queue items are consumed. If
38 *maxsize* is less than or equal to zero, the queue size is infinite.
39
Christian Heimes679db4a2008-01-18 09:56:22 +000040
Raymond Hettinger35641462008-01-17 00:13:27 +000041.. class:: PriorityQueue(maxsize)
42
43 Constructor for a priority queue. *maxsize* is an integer that sets the upperbound
44 limit on the number of items that can be placed in the queue. Insertion will
45 block once this size has been reached, until queue items are consumed. If
46 *maxsize* is less than or equal to zero, the queue size is infinite.
47
48 The lowest valued entries are retrieved first (the lowest valued entry is the
49 one returned by ``sorted(list(entries))[0]``). A typical pattern for entries
50 is a tuple in the form: ``(priority_number, data)``.
Georg Brandl116aa622007-08-15 14:28:22 +000051
Christian Heimes679db4a2008-01-18 09:56:22 +000052
Georg Brandl116aa622007-08-15 14:28:22 +000053.. exception:: Empty
54
55 Exception raised when non-blocking :meth:`get` (or :meth:`get_nowait`) is called
56 on a :class:`Queue` object which is empty.
57
58
59.. exception:: Full
60
61 Exception raised when non-blocking :meth:`put` (or :meth:`put_nowait`) is called
62 on a :class:`Queue` object which is full.
63
64
65.. _queueobjects:
66
67Queue Objects
68-------------
69
Christian Heimes292d3512008-02-03 16:51:08 +000070Queue objects (:class:`Queue`, :class:`LifoQueue`, or :class:`PriorityQueue`)
Raymond Hettinger35641462008-01-17 00:13:27 +000071provide the public methods described below.
Georg Brandl116aa622007-08-15 14:28:22 +000072
73
74.. method:: Queue.qsize()
75
Guido van Rossum7736b5b2008-01-15 21:44:53 +000076 Return the approximate size of the queue. Note, qsize() > 0 doesn't
77 guarantee that a subsequent get() will not block, nor will qsize() < maxsize
78 guarantee that put() will not block.
Georg Brandl116aa622007-08-15 14:28:22 +000079
80
Georg Brandl116aa622007-08-15 14:28:22 +000081.. method:: Queue.put(item[, block[, timeout]])
82
83 Put *item* into the queue. If optional args *block* is true and *timeout* is
84 None (the default), block if necessary until a free slot is available. If
85 *timeout* is a positive number, it blocks at most *timeout* seconds and raises
86 the :exc:`Full` exception if no free slot was available within that time.
87 Otherwise (*block* is false), put an item on the queue if a free slot is
88 immediately available, else raise the :exc:`Full` exception (*timeout* is
89 ignored in that case).
90
Georg Brandl116aa622007-08-15 14:28:22 +000091
92.. method:: Queue.put_nowait(item)
93
94 Equivalent to ``put(item, False)``.
95
96
97.. method:: Queue.get([block[, timeout]])
98
99 Remove and return an item from the queue. If optional args *block* is true and
100 *timeout* is None (the default), block if necessary until an item is available.
101 If *timeout* is a positive number, it blocks at most *timeout* seconds and
102 raises the :exc:`Empty` exception if no item was available within that time.
103 Otherwise (*block* is false), return an item if one is immediately available,
104 else raise the :exc:`Empty` exception (*timeout* is ignored in that case).
105
Georg Brandl116aa622007-08-15 14:28:22 +0000106
107.. method:: Queue.get_nowait()
108
109 Equivalent to ``get(False)``.
110
111Two methods are offered to support tracking whether enqueued tasks have been
112fully processed by daemon consumer threads.
113
114
115.. method:: Queue.task_done()
116
117 Indicate that a formerly enqueued task is complete. Used by queue consumer
118 threads. For each :meth:`get` used to fetch a task, a subsequent call to
119 :meth:`task_done` tells the queue that the processing on the task is complete.
120
121 If a :meth:`join` is currently blocking, it will resume when all items have been
122 processed (meaning that a :meth:`task_done` call was received for every item
123 that had been :meth:`put` into the queue).
124
125 Raises a :exc:`ValueError` if called more times than there were items placed in
126 the queue.
127
Georg Brandl116aa622007-08-15 14:28:22 +0000128
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
Georg Brandl116aa622007-08-15 14:28:22 +0000138
139Example of how to wait for enqueued tasks to be completed::
140
141 def worker():
142 while True:
143 item = q.get()
144 do_work(item)
145 q.task_done()
146
147 q = Queue()
148 for i in range(num_worker_threads):
149 t = Thread(target=worker)
150 t.setDaemon(True)
151 t.start()
152
153 for item in source():
154 q.put(item)
155
156 q.join() # block until all tasks are done
157