blob: 667d81303ee0b12d5c1c9e40614d10bfe7dbdc1c [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{Queue} ---
2 A synchronized queue class.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{Queue}
4
Fred Drake295da241998-08-10 19:42:37 +00005\modulesynopsis{A synchronized queue class.}
Fred Drakeb91e9341998-07-23 17:59:49 +00006
Barry Warsaw17c8e781997-11-20 19:54:16 +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}
28Exception raised when non-blocking get (e.g. \method{get_nowait()}) is
29called on a \class{Queue} object which is empty, or for which the
30emptyiness cannot be determined (i.e. because the appropriate locks
31cannot be acquired).
32\end{excdesc}
33
34\subsection{Queue Objects}
35\label{QueueObjects}
36
37Class \class{Queue} implements queue objects and has the methods
38described below. This class can be derived from in order to implement
39other queue organizations (e.g. stack) but the inheritable interface
40is not described here. See the source code for details. The public
41methods are:
42
Fred Drake8fe533e1998-03-27 05:27:08 +000043\begin{methoddesc}{qsize}{}
Barry Warsaw17c8e781997-11-20 19:54:16 +000044Returns the approximate size of the queue. Because of multithreading
45semantics, this number is not reliable.
Fred Drake8fe533e1998-03-27 05:27:08 +000046\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000047
Fred Drake8fe533e1998-03-27 05:27:08 +000048\begin{methoddesc}{empty}{}
Fred Drake4ef33291998-03-10 05:32:30 +000049Returns \code{1} if the queue is empty, \code{0} otherwise. Because
50of multithreading semantics, this 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}{full}{}
Fred Drake4ef33291998-03-10 05:32:30 +000054Returns \code{1} if the queue is full, \code{0} otherwise. Because of
Barry Warsaw17c8e781997-11-20 19:54:16 +000055multithreading 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}{put}{item}
Barry Warsaw17c8e781997-11-20 19:54:16 +000059Puts \var{item} into the queue.
Fred Drake8fe533e1998-03-27 05:27:08 +000060\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000061
Fred Drake8fe533e1998-03-27 05:27:08 +000062\begin{methoddesc}{get}{}
Barry Warsaw17c8e781997-11-20 19:54:16 +000063Gets and returns an item from the queue, blocking if necessary until
64one is available.
Fred Drake8fe533e1998-03-27 05:27:08 +000065\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000066
Fred Drake8fe533e1998-03-27 05:27:08 +000067\begin{methoddesc}{get_nowait}{}
Barry Warsaw17c8e781997-11-20 19:54:16 +000068Gets and returns an item from the queue if one is immediately
Fred Drake4ef33291998-03-10 05:32:30 +000069available. Raises an \exception{Empty} exception if the queue is
70empty or if the queue's emptiness cannot be determined.
Fred Drake8fe533e1998-03-27 05:27:08 +000071\end{methoddesc}