blob: a846036fe45d33ba228057f039687b6538506a1a [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}
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
30empty or locked.
31\end{excdesc}
32
33\begin{excdesc}{Full}
34Exception raised when non-blocking \method{put()} (or
35\method{get_nowait()}) is called on a \class{Queue} object which is
36full or locked.
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}{}
Guido van Rossumce67f061999-02-08 18:43:13 +000054Return \code{1} if the queue is empty, \code{0} otherwise. Because
Fred Drake4ef33291998-03-10 05:32:30 +000055of 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}{}
Guido van Rossumce67f061999-02-08 18:43:13 +000059Return \code{1} if the queue is full, \code{0} otherwise. Because of
Barry Warsaw17c8e781997-11-20 19:54:16 +000060multithreading semantics, this is not reliable.
Fred Drake8fe533e1998-03-27 05:27:08 +000061\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000062
Guido van Rossumce67f061999-02-08 18:43:13 +000063\begin{methoddesc}{put}{item\optional{, block}}
64Put \var{item} into the queue. If optional argument \var{block} is 1
65(the default), block if necessary until a free slot is available.
66Otherwise (\var{block} is 0), put \var{item} on the queue if a free
67slot is immediately available, else raise the \exception{Full}
68exception.
Fred Drake8fe533e1998-03-27 05:27:08 +000069\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000070
Guido van Rossumce67f061999-02-08 18:43:13 +000071\begin{methoddesc}{put_nowait}{item}
72Equivalent to \code{put(\var{item}, 0)}.
73\end{methoddesc}
74
75\begin{methoddesc}{get}{\optional{block}}
76Remove and return an item from the queue. If optional argument
77\var{block} is 1 (the default), block if necessary until an item is
78available. Otherwise (\var{block} is 0), return an item if one is
79immediately available, else raise the
80\exception{Empty} exception.
Fred Drake8fe533e1998-03-27 05:27:08 +000081\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000082
Fred Drake8fe533e1998-03-27 05:27:08 +000083\begin{methoddesc}{get_nowait}{}
Guido van Rossumce67f061999-02-08 18:43:13 +000084Equivalent to \code{get(0)}.
Fred Drake8fe533e1998-03-27 05:27:08 +000085\end{methoddesc}