blob: 3231e11b337e830735d72317493486e94699dcc7 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{Queue}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{standard}{Queue}
3
4\modulesynopsis{A stynchronized queue class.}
5
Barry Warsaw17c8e781997-11-20 19:54:16 +00006
Barry Warsaw17c8e781997-11-20 19:54:16 +00007
Fred Drake4ef33291998-03-10 05:32:30 +00008The \module{Queue} module implements a multi-producer, multi-consumer
Barry Warsaw17c8e781997-11-20 19:54:16 +00009FIFO queue. It is especially useful in threads programming when
10information must be exchanged safely between multiple threads. The
Fred Drake4ef33291998-03-10 05:32:30 +000011\class{Queue} class in this module implements all the required locking
Barry Warsaw17c8e781997-11-20 19:54:16 +000012semantics. It depends on the availability of thread support in
13Python.
14
Fred Drake4ef33291998-03-10 05:32:30 +000015The \module{Queue} module defines the following class and exception:
Barry Warsaw17c8e781997-11-20 19:54:16 +000016
Barry Warsaw17c8e781997-11-20 19:54:16 +000017
Fred Drake4ef33291998-03-10 05:32:30 +000018\begin{classdesc}{Queue}{maxsize}
Barry Warsaw17c8e781997-11-20 19:54:16 +000019Constructor for the class. \var{maxsize} is an integer that sets the
20upperbound limit on the number of items that can be placed in the
21queue. Insertion will block once this size has been reached, until
22queue items are consumed. If \var{maxsize} is less than or equal to
23zero, the queue size is infinite.
Fred Drake4ef33291998-03-10 05:32:30 +000024\end{classdesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000025
Fred Drake4ef33291998-03-10 05:32:30 +000026\begin{excdesc}{Empty}
27Exception raised when non-blocking get (e.g. \method{get_nowait()}) is
28called on a \class{Queue} object which is empty, or for which the
29emptyiness cannot be determined (i.e. because the appropriate locks
30cannot be acquired).
31\end{excdesc}
32
33\subsection{Queue Objects}
34\label{QueueObjects}
35
36Class \class{Queue} implements queue objects and has the methods
37described below. This class can be derived from in order to implement
38other queue organizations (e.g. stack) but the inheritable interface
39is not described here. See the source code for details. The public
40methods are:
41
Fred Drake8fe533e1998-03-27 05:27:08 +000042\begin{methoddesc}{qsize}{}
Barry Warsaw17c8e781997-11-20 19:54:16 +000043Returns the approximate size of the queue. Because of multithreading
44semantics, this number is not reliable.
Fred Drake8fe533e1998-03-27 05:27:08 +000045\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000046
Fred Drake8fe533e1998-03-27 05:27:08 +000047\begin{methoddesc}{empty}{}
Fred Drake4ef33291998-03-10 05:32:30 +000048Returns \code{1} if the queue is empty, \code{0} otherwise. Because
49of multithreading semantics, this is not reliable.
Fred Drake8fe533e1998-03-27 05:27:08 +000050\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000051
Fred Drake8fe533e1998-03-27 05:27:08 +000052\begin{methoddesc}{full}{}
Fred Drake4ef33291998-03-10 05:32:30 +000053Returns \code{1} if the queue is full, \code{0} otherwise. Because of
Barry Warsaw17c8e781997-11-20 19:54:16 +000054multithreading semantics, this is not reliable.
Fred Drake8fe533e1998-03-27 05:27:08 +000055\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000056
Fred Drake8fe533e1998-03-27 05:27:08 +000057\begin{methoddesc}{put}{item}
Barry Warsaw17c8e781997-11-20 19:54:16 +000058Puts \var{item} into the queue.
Fred Drake8fe533e1998-03-27 05:27:08 +000059\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000060
Fred Drake8fe533e1998-03-27 05:27:08 +000061\begin{methoddesc}{get}{}
Barry Warsaw17c8e781997-11-20 19:54:16 +000062Gets and returns an item from the queue, blocking if necessary until
63one is available.
Fred Drake8fe533e1998-03-27 05:27:08 +000064\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000065
Fred Drake8fe533e1998-03-27 05:27:08 +000066\begin{methoddesc}{get_nowait}{}
Barry Warsaw17c8e781997-11-20 19:54:16 +000067Gets and returns an item from the queue if one is immediately
Fred Drake4ef33291998-03-10 05:32:30 +000068available. Raises an \exception{Empty} exception if the queue is
69empty or if the queue's emptiness cannot be determined.
Fred Drake8fe533e1998-03-27 05:27:08 +000070\end{methoddesc}