blob: 95ad47f47a7b6213ca4ab458d3ab484b139c14d2 [file] [log] [blame]
Raymond Hettingerfd3fcf02006-03-24 20:43:29 +00001
Fred Drake295da241998-08-10 19:42:37 +00002\section{\module{Queue} ---
Fred Drakef8ca7d82000-10-10 17:03:45 +00003 A synchronized queue class}
4
Fred Drakeb91e9341998-07-23 17:59:49 +00005\declaremodule{standard}{Queue}
Fred Drake295da241998-08-10 19:42:37 +00006\modulesynopsis{A synchronized queue class.}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Barry Warsaw17c8e781997-11-20 19:54:16 +00008
Fred Drake4ef33291998-03-10 05:32:30 +00009The \module{Queue} module implements a multi-producer, multi-consumer
Barry Warsaw17c8e781997-11-20 19:54:16 +000010FIFO queue. It is especially useful in threads programming when
11information must be exchanged safely between multiple threads. The
Fred Drake4ef33291998-03-10 05:32:30 +000012\class{Queue} class in this module implements all the required locking
Barry Warsaw17c8e781997-11-20 19:54:16 +000013semantics. It depends on the availability of thread support in
14Python.
15
Fred Drake4ef33291998-03-10 05:32:30 +000016The \module{Queue} module defines the following class and exception:
Barry Warsaw17c8e781997-11-20 19:54:16 +000017
Barry Warsaw17c8e781997-11-20 19:54:16 +000018
Fred Drake4ef33291998-03-10 05:32:30 +000019\begin{classdesc}{Queue}{maxsize}
Barry Warsaw17c8e781997-11-20 19:54:16 +000020Constructor 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.
Fred Drake4ef33291998-03-10 05:32:30 +000025\end{classdesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000026
Fred Drake4ef33291998-03-10 05:32:30 +000027\begin{excdesc}{Empty}
Guido van Rossumce67f061999-02-08 18:43:13 +000028Exception raised when non-blocking \method{get()} (or
29\method{get_nowait()}) is called on a \class{Queue} object which is
Tim Peters5af0e412004-07-12 00:45:14 +000030empty.
Guido van Rossumce67f061999-02-08 18:43:13 +000031\end{excdesc}
32
33\begin{excdesc}{Full}
34Exception raised when non-blocking \method{put()} (or
Fred Drake38e5d272000-04-03 20:13:55 +000035\method{put_nowait()}) is called on a \class{Queue} object which is
Tim Peters5af0e412004-07-12 00:45:14 +000036full.
Fred Drake4ef33291998-03-10 05:32:30 +000037\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
Fred Drake8fe533e1998-03-27 05:27:08 +000048\begin{methoddesc}{qsize}{}
Guido van Rossumce67f061999-02-08 18:43:13 +000049Return the approximate size of the queue. Because of multithreading
Barry Warsaw17c8e781997-11-20 19:54:16 +000050semantics, this number is not reliable.
Fred Drake8fe533e1998-03-27 05:27:08 +000051\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000052
Fred Drake8fe533e1998-03-27 05:27:08 +000053\begin{methoddesc}{empty}{}
Martin v. Löwis77ac4292002-10-15 15:11:13 +000054Return \code{True} if the queue is empty, \code{False} otherwise.
Tim Peters5af0e412004-07-12 00:45:14 +000055Because of multithreading semantics, this is not reliable.
Fred Drake8fe533e1998-03-27 05:27:08 +000056\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000057
Fred Drake8fe533e1998-03-27 05:27:08 +000058\begin{methoddesc}{full}{}
Martin v. Löwis77ac4292002-10-15 15:11:13 +000059Return \code{True} if the queue is full, \code{False} otherwise.
60Because of multithreading semantics, this is not reliable.
Fred Drake8fe533e1998-03-27 05:27:08 +000061\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000062
Martin v. Löwis77ac4292002-10-15 15:11:13 +000063\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
Guido van Rossumce67f061999-02-08 18:43:13 +000070slot is immediately available, else raise the \exception{Full}
Martin v. Löwis77ac4292002-10-15 15:11:13 +000071exception (\var{timeout} is ignored in that case).
72
73\versionadded[the timeout parameter]{2.3}
74
Fred Drake8fe533e1998-03-27 05:27:08 +000075\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000076
Guido van Rossumce67f061999-02-08 18:43:13 +000077\begin{methoddesc}{put_nowait}{item}
Martin v. Löwis77ac4292002-10-15 15:11:13 +000078Equivalent to \code{put(\var{item}, False)}.
Guido van Rossumce67f061999-02-08 18:43:13 +000079\end{methoddesc}
80
Martin v. Löwis77ac4292002-10-15 15:11:13 +000081\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
Fred Drake8fe533e1998-03-27 05:27:08 +000093\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000094
Fred Drake8fe533e1998-03-27 05:27:08 +000095\begin{methoddesc}{get_nowait}{}
Martin v. Löwis77ac4292002-10-15 15:11:13 +000096Equivalent to \code{get(False)}.
Fred Drake8fe533e1998-03-27 05:27:08 +000097\end{methoddesc}
Raymond Hettingerfd3fcf02006-03-24 20:43:29 +000098
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}