blob: f1d892ad53da2a9b759ebe5900b7a7796b750187 [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
Tim Peters5af0e412004-07-12 00:45:14 +000029empty.
Guido van Rossumce67f061999-02-08 18:43:13 +000030\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
Tim Peters5af0e412004-07-12 00:45:14 +000035full.
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}{}
Martin v. Löwis77ac4292002-10-15 15:11:13 +000053Return \code{True} if the queue is empty, \code{False} otherwise.
Tim Peters5af0e412004-07-12 00:45:14 +000054Because of 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}{}
Martin v. Löwis77ac4292002-10-15 15:11:13 +000058Return \code{True} if the queue is full, \code{False} otherwise.
59Because of multithreading semantics, this is not reliable.
Fred Drake8fe533e1998-03-27 05:27:08 +000060\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000061
Martin v. Löwis77ac4292002-10-15 15:11:13 +000062\begin{methoddesc}{put}{item\optional{, block\optional{, timeout}}}
63Put \var{item} into the queue. If optional args \var{block} is true
64and \var{timeout} is None (the default), block if necessary until a
65free slot is available. If \var{timeout} is a positive number, it
66blocks at most \var{timeout} seconds and raises the \exception{Full}
67exception if no free slot was available within that time.
68Otherwise (\var{block} is false), put an item on the queue if a free
Guido van Rossumce67f061999-02-08 18:43:13 +000069slot is immediately available, else raise the \exception{Full}
Martin v. Löwis77ac4292002-10-15 15:11:13 +000070exception (\var{timeout} is ignored in that case).
71
72\versionadded[the timeout parameter]{2.3}
73
Fred Drake8fe533e1998-03-27 05:27:08 +000074\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000075
Guido van Rossumce67f061999-02-08 18:43:13 +000076\begin{methoddesc}{put_nowait}{item}
Martin v. Löwis77ac4292002-10-15 15:11:13 +000077Equivalent to \code{put(\var{item}, False)}.
Guido van Rossumce67f061999-02-08 18:43:13 +000078\end{methoddesc}
79
Martin v. Löwis77ac4292002-10-15 15:11:13 +000080\begin{methoddesc}{get}{\optional{block\optional{, timeout}}}
81Remove and return an item from the queue. If optional args
82\var{block} is true and \var{timeout} is None (the default),
83block if necessary until an item is available. If \var{timeout} is
84a positive number, it blocks at most \var{timeout} seconds and raises
85the \exception{Empty} exception if no item was available within that
86time. Otherwise (\var{block} is false), return an item if one is
87immediately available, else raise the \exception{Empty} exception
88(\var{timeout} is ignored in that case).
89
90\versionadded[the timeout parameter]{2.3}
91
Fred Drake8fe533e1998-03-27 05:27:08 +000092\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000093
Fred Drake8fe533e1998-03-27 05:27:08 +000094\begin{methoddesc}{get_nowait}{}
Martin v. Löwis77ac4292002-10-15 15:11:13 +000095Equivalent to \code{get(False)}.
Fred Drake8fe533e1998-03-27 05:27:08 +000096\end{methoddesc}