blob: c5dbc2095c9b93b10fbd2d31d1f4ded6b1ec2163 [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.
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
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
52 Return the approximate size of the queue. Because of multithreading semantics,
53 this number is not reliable.
54
55
Georg Brandl116aa622007-08-15 14:28:22 +000056.. method:: Queue.put(item[, block[, timeout]])
57
58 Put *item* into the queue. If optional args *block* is true and *timeout* is
59 None (the default), block if necessary until a free slot is available. If
60 *timeout* is a positive number, it blocks at most *timeout* seconds and raises
61 the :exc:`Full` exception if no free slot was available within that time.
62 Otherwise (*block* is false), put an item on the queue if a free slot is
63 immediately available, else raise the :exc:`Full` exception (*timeout* is
64 ignored in that case).
65
Georg Brandl116aa622007-08-15 14:28:22 +000066
67.. method:: Queue.put_nowait(item)
68
69 Equivalent to ``put(item, False)``.
70
71
72.. method:: Queue.get([block[, timeout]])
73
74 Remove and return an item from the queue. If optional args *block* is true and
75 *timeout* is None (the default), block if necessary until an item is available.
76 If *timeout* is a positive number, it blocks at most *timeout* seconds and
77 raises the :exc:`Empty` exception if no item was available within that time.
78 Otherwise (*block* is false), return an item if one is immediately available,
79 else raise the :exc:`Empty` exception (*timeout* is ignored in that case).
80
Georg Brandl116aa622007-08-15 14:28:22 +000081
82.. method:: Queue.get_nowait()
83
84 Equivalent to ``get(False)``.
85
86Two methods are offered to support tracking whether enqueued tasks have been
87fully processed by daemon consumer threads.
88
89
90.. method:: Queue.task_done()
91
92 Indicate that a formerly enqueued task is complete. Used by queue consumer
93 threads. For each :meth:`get` used to fetch a task, a subsequent call to
94 :meth:`task_done` tells the queue that the processing on the task is complete.
95
96 If a :meth:`join` is currently blocking, it will resume when all items have been
97 processed (meaning that a :meth:`task_done` call was received for every item
98 that had been :meth:`put` into the queue).
99
100 Raises a :exc:`ValueError` if called more times than there were items placed in
101 the queue.
102
Georg Brandl116aa622007-08-15 14:28:22 +0000103
104.. method:: Queue.join()
105
106 Blocks until all items in the queue have been gotten and processed.
107
108 The count of unfinished tasks goes up whenever an item is added to the queue.
109 The count goes down whenever a consumer thread calls :meth:`task_done` to
110 indicate that the item was retrieved and all work on it is complete. When the
111 count of unfinished tasks drops to zero, join() unblocks.
112
Georg Brandl116aa622007-08-15 14:28:22 +0000113
114Example of how to wait for enqueued tasks to be completed::
115
116 def worker():
117 while True:
118 item = q.get()
119 do_work(item)
120 q.task_done()
121
122 q = Queue()
123 for i in range(num_worker_threads):
124 t = Thread(target=worker)
125 t.setDaemon(True)
126 t.start()
127
128 for item in source():
129 q.put(item)
130
131 q.join() # block until all tasks are done
132