blob: 562623a75dad05ab48614d8c03b6f96dd0643a8f [file] [log] [blame]
Fred Drake9c483191997-12-17 14:08:01 +00001\section{Standard Module \sectcode{Queue}}
Barry Warsaw17c8e781997-11-20 19:54:16 +00002\stmodindex{Queue}
Barry Warsaw17c8e781997-11-20 19:54:16 +00003\label{module-Queue}
4
Barry Warsaw17c8e781997-11-20 19:54:16 +00005
Fred Drake4ef33291998-03-10 05:32:30 +00006The \module{Queue} module implements a multi-producer, multi-consumer
Barry Warsaw17c8e781997-11-20 19:54:16 +00007FIFO queue. It is especially useful in threads programming when
8information must be exchanged safely between multiple threads. The
Fred Drake4ef33291998-03-10 05:32:30 +00009\class{Queue} class in this module implements all the required locking
Barry Warsaw17c8e781997-11-20 19:54:16 +000010semantics. It depends on the availability of thread support in
11Python.
12
Fred Drake4ef33291998-03-10 05:32:30 +000013The \module{Queue} module defines the following class and exception:
Barry Warsaw17c8e781997-11-20 19:54:16 +000014
Barry Warsaw17c8e781997-11-20 19:54:16 +000015
Fred Drake4ef33291998-03-10 05:32:30 +000016\begin{classdesc}{Queue}{maxsize}
Barry Warsaw17c8e781997-11-20 19:54:16 +000017Constructor for the class. \var{maxsize} is an integer that sets the
18upperbound limit on the number of items that can be placed in the
19queue. Insertion will block once this size has been reached, until
20queue items are consumed. If \var{maxsize} is less than or equal to
21zero, the queue size is infinite.
Fred Drake4ef33291998-03-10 05:32:30 +000022\end{classdesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000023
Fred Drake4ef33291998-03-10 05:32:30 +000024\begin{excdesc}{Empty}
25Exception raised when non-blocking get (e.g. \method{get_nowait()}) is
26called on a \class{Queue} object which is empty, or for which the
27emptyiness cannot be determined (i.e. because the appropriate locks
28cannot be acquired).
29\end{excdesc}
30
31\subsection{Queue Objects}
32\label{QueueObjects}
33
34Class \class{Queue} implements queue objects and has the methods
35described below. This class can be derived from in order to implement
36other queue organizations (e.g. stack) but the inheritable interface
37is not described here. See the source code for details. The public
38methods are:
39
40\setindexsubitem{(Queue method)}
Barry Warsaw17c8e781997-11-20 19:54:16 +000041
42\begin{funcdesc}{qsize}{}
43Returns the approximate size of the queue. Because of multithreading
44semantics, this number is not reliable.
45\end{funcdesc}
46
Barry Warsaw17c8e781997-11-20 19:54:16 +000047\begin{funcdesc}{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.
Barry Warsaw17c8e781997-11-20 19:54:16 +000050\end{funcdesc}
51
Barry Warsaw17c8e781997-11-20 19:54:16 +000052\begin{funcdesc}{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.
55\end{funcdesc}
56
Barry Warsaw17c8e781997-11-20 19:54:16 +000057\begin{funcdesc}{put}{item}
58Puts \var{item} into the queue.
59\end{funcdesc}
60
Barry Warsaw17c8e781997-11-20 19:54:16 +000061\begin{funcdesc}{get}{}
62Gets and returns an item from the queue, blocking if necessary until
63one is available.
64\end{funcdesc}
65
Barry Warsaw17c8e781997-11-20 19:54:16 +000066\begin{funcdesc}{get_nowait}{}
67Gets 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.
Barry Warsaw17c8e781997-11-20 19:54:16 +000070\end{funcdesc}