blob: 0abed61c8383c482ed1ddbcd778e844d189f208a [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{Queue} ---
Fred Drakef8ca7d82000-10-10 17:03:45 +00002 A synchronized queue class}
3
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{standard}{Queue}
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
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}
Guido van Rossumce67f061999-02-08 18:43:13 +000027Exception raised when non-blocking \method{get()} (or
28\method{get_nowait()}) is called on a \class{Queue} object which is
29empty or locked.
30\end{excdesc}
31
32\begin{excdesc}{Full}
33Exception raised when non-blocking \method{put()} (or
Fred Drake38e5d272000-04-03 20:13:55 +000034\method{put_nowait()}) is called on a \class{Queue} object which is
Guido van Rossumce67f061999-02-08 18:43:13 +000035full or locked.
Fred Drake4ef33291998-03-10 05:32:30 +000036\end{excdesc}
37
38\subsection{Queue Objects}
39\label{QueueObjects}
40
41Class \class{Queue} implements queue objects and has the methods
42described below. This class can be derived from in order to implement
43other queue organizations (e.g. stack) but the inheritable interface
44is not described here. See the source code for details. The public
45methods are:
46
Fred Drake8fe533e1998-03-27 05:27:08 +000047\begin{methoddesc}{qsize}{}
Guido van Rossumce67f061999-02-08 18:43:13 +000048Return the approximate size of the queue. Because of multithreading
Barry Warsaw17c8e781997-11-20 19:54:16 +000049semantics, this number 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}{empty}{}
Guido van Rossumce67f061999-02-08 18:43:13 +000053Return \code{1} if the queue is empty, \code{0} otherwise. Because
Fred Drake4ef33291998-03-10 05:32:30 +000054of multithreading 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}{full}{}
Guido van Rossumce67f061999-02-08 18:43:13 +000058Return \code{1} if the queue is full, \code{0} otherwise. Because of
Barry Warsaw17c8e781997-11-20 19:54:16 +000059multithreading semantics, this is not reliable.
Fred Drake8fe533e1998-03-27 05:27:08 +000060\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000061
Guido van Rossumce67f061999-02-08 18:43:13 +000062\begin{methoddesc}{put}{item\optional{, block}}
63Put \var{item} into the queue. If optional argument \var{block} is 1
64(the default), block if necessary until a free slot is available.
65Otherwise (\var{block} is 0), put \var{item} on the queue if a free
66slot is immediately available, else raise the \exception{Full}
67exception.
Fred Drake8fe533e1998-03-27 05:27:08 +000068\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000069
Guido van Rossumce67f061999-02-08 18:43:13 +000070\begin{methoddesc}{put_nowait}{item}
71Equivalent to \code{put(\var{item}, 0)}.
72\end{methoddesc}
73
74\begin{methoddesc}{get}{\optional{block}}
75Remove and return an item from the queue. If optional argument
76\var{block} is 1 (the default), block if necessary until an item is
77available. Otherwise (\var{block} is 0), return an item if one is
78immediately available, else raise the
79\exception{Empty} exception.
Fred Drake8fe533e1998-03-27 05:27:08 +000080\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000081
Fred Drake8fe533e1998-03-27 05:27:08 +000082\begin{methoddesc}{get_nowait}{}
Guido van Rossumce67f061999-02-08 18:43:13 +000083Equivalent to \code{get(0)}.
Fred Drake8fe533e1998-03-27 05:27:08 +000084\end{methoddesc}