source: vendor/python/2.5/Doc/lib/libqueue.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 5.0 KB
Line 
1
2\section{\module{Queue} ---
3 A synchronized queue class}
4
5\declaremodule{standard}{Queue}
6\modulesynopsis{A synchronized queue class.}
7
8
9The \module{Queue} module implements a multi-producer, multi-consumer
10FIFO queue. It is especially useful in threads programming when
11information must be exchanged safely between multiple threads. The
12\class{Queue} class in this module implements all the required locking
13semantics. It depends on the availability of thread support in
14Python.
15
16The \module{Queue} module defines the following class and exception:
17
18
19\begin{classdesc}{Queue}{maxsize}
20Constructor for the class. \var{maxsize} is an integer that sets the
21upperbound limit on the number of items that can be placed in the
22queue. Insertion will block once this size has been reached, until
23queue items are consumed. If \var{maxsize} is less than or equal to
24zero, the queue size is infinite.
25\end{classdesc}
26
27\begin{excdesc}{Empty}
28Exception raised when non-blocking \method{get()} (or
29\method{get_nowait()}) is called on a \class{Queue} object which is
30empty.
31\end{excdesc}
32
33\begin{excdesc}{Full}
34Exception raised when non-blocking \method{put()} (or
35\method{put_nowait()}) is called on a \class{Queue} object which is
36full.
37\end{excdesc}
38
39\subsection{Queue Objects}
40\label{QueueObjects}
41
42Class \class{Queue} implements queue objects and has the methods
43described below. This class can be derived from in order to implement
44other queue organizations (e.g. stack) but the inheritable interface
45is not described here. See the source code for details. The public
46methods are:
47
48\begin{methoddesc}{qsize}{}
49Return the approximate size of the queue. Because of multithreading
50semantics, this number is not reliable.
51\end{methoddesc}
52
53\begin{methoddesc}{empty}{}
54Return \code{True} if the queue is empty, \code{False} otherwise.
55Because of multithreading semantics, this is not reliable.
56\end{methoddesc}
57
58\begin{methoddesc}{full}{}
59Return \code{True} if the queue is full, \code{False} otherwise.
60Because of multithreading semantics, this is not reliable.
61\end{methoddesc}
62
63\begin{methoddesc}{put}{item\optional{, block\optional{, timeout}}}
64Put \var{item} into the queue. If optional args \var{block} is true
65and \var{timeout} is None (the default), block if necessary until a
66free slot is available. If \var{timeout} is a positive number, it
67blocks at most \var{timeout} seconds and raises the \exception{Full}
68exception if no free slot was available within that time.
69Otherwise (\var{block} is false), put an item on the queue if a free
70slot is immediately available, else raise the \exception{Full}
71exception (\var{timeout} is ignored in that case).
72
73\versionadded[the timeout parameter]{2.3}
74
75\end{methoddesc}
76
77\begin{methoddesc}{put_nowait}{item}
78Equivalent to \code{put(\var{item}, False)}.
79\end{methoddesc}
80
81\begin{methoddesc}{get}{\optional{block\optional{, timeout}}}
82Remove and return an item from the queue. If optional args
83\var{block} is true and \var{timeout} is None (the default),
84block if necessary until an item is available. If \var{timeout} is
85a positive number, it blocks at most \var{timeout} seconds and raises
86the \exception{Empty} exception if no item was available within that
87time. Otherwise (\var{block} is false), return an item if one is
88immediately available, else raise the \exception{Empty} exception
89(\var{timeout} is ignored in that case).
90
91\versionadded[the timeout parameter]{2.3}
92
93\end{methoddesc}
94
95\begin{methoddesc}{get_nowait}{}
96Equivalent to \code{get(False)}.
97\end{methoddesc}
98
99Two methods are offered to support tracking whether enqueued tasks have
100been fully processed by daemon consumer threads.
101
102\begin{methoddesc}{task_done}{}
103Indicate that a formerly enqueued task is complete. Used by queue consumer
104threads. For each \method{get()} used to fetch a task, a subsequent call to
105\method{task_done()} tells the queue that the processing on the task is complete.
106
107If a \method{join()} is currently blocking, it will resume when all items
108have been processed (meaning that a \method{task_done()} call was received
109for every item that had been \method{put()} into the queue).
110
111Raises a \exception{ValueError} if called more times than there were items
112placed in the queue.
113\versionadded{2.5}
114\end{methoddesc}
115
116\begin{methoddesc}{join}{}
117Blocks until all items in the queue have been gotten and processed.
118
119The count of unfinished tasks goes up whenever an item is added to the
120queue. The count goes down whenever a consumer thread calls \method{task_done()}
121to indicate that the item was retrieved and all work on it is complete.
122When the count of unfinished tasks drops to zero, join() unblocks.
123\versionadded{2.5}
124\end{methoddesc}
125
126Example of how to wait for enqueued tasks to be completed:
127
128\begin{verbatim}
129 def worker():
130 while True:
131 item = q.get()
132 do_work(item)
133 q.task_done()
134
135 q = Queue()
136 for i in range(num_worker_threads):
137 t = Thread(target=worker)
138 t.setDaemon(True)
139 t.start()
140
141 for item in source():
142 q.put(item)
143
144 q.join() # block until all tasks are done
145\end{verbatim}
Note: See TracBrowser for help on using the repository browser.