blob: db6cabc1f14544cb3acf568920e5830fa1679716 [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
Fred Drake8fe533e1998-03-27 05:27:08 +000040\begin{methoddesc}{qsize}{}
Barry Warsaw17c8e781997-11-20 19:54:16 +000041Returns the approximate size of the queue. Because of multithreading
42semantics, this number is not reliable.
Fred Drake8fe533e1998-03-27 05:27:08 +000043\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000044
Fred Drake8fe533e1998-03-27 05:27:08 +000045\begin{methoddesc}{empty}{}
Fred Drake4ef33291998-03-10 05:32:30 +000046Returns \code{1} if the queue is empty, \code{0} otherwise. Because
47of multithreading semantics, this is not reliable.
Fred Drake8fe533e1998-03-27 05:27:08 +000048\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000049
Fred Drake8fe533e1998-03-27 05:27:08 +000050\begin{methoddesc}{full}{}
Fred Drake4ef33291998-03-10 05:32:30 +000051Returns \code{1} if the queue is full, \code{0} otherwise. Because of
Barry Warsaw17c8e781997-11-20 19:54:16 +000052multithreading semantics, this is not reliable.
Fred Drake8fe533e1998-03-27 05:27:08 +000053\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000054
Fred Drake8fe533e1998-03-27 05:27:08 +000055\begin{methoddesc}{put}{item}
Barry Warsaw17c8e781997-11-20 19:54:16 +000056Puts \var{item} into the queue.
Fred Drake8fe533e1998-03-27 05:27:08 +000057\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000058
Fred Drake8fe533e1998-03-27 05:27:08 +000059\begin{methoddesc}{get}{}
Barry Warsaw17c8e781997-11-20 19:54:16 +000060Gets and returns an item from the queue, blocking if necessary until
61one is available.
Fred Drake8fe533e1998-03-27 05:27:08 +000062\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000063
Fred Drake8fe533e1998-03-27 05:27:08 +000064\begin{methoddesc}{get_nowait}{}
Barry Warsaw17c8e781997-11-20 19:54:16 +000065Gets and returns an item from the queue if one is immediately
Fred Drake4ef33291998-03-10 05:32:30 +000066available. Raises an \exception{Empty} exception if the queue is
67empty or if the queue's emptiness cannot be determined.
Fred Drake8fe533e1998-03-27 05:27:08 +000068\end{methoddesc}