blob: 0770bfe8ab61096357e2c3c87be15c2051e4dd4e [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
Skip Montanaro763805d2002-06-26 05:22:08 +000015\begin{seealso}
16 \seemodule{bisect}{PriorityQueue example using the Queue class}
17\end{seealso}
18
Fred Drake4ef33291998-03-10 05:32:30 +000019The \module{Queue} module defines the following class and exception:
Barry Warsaw17c8e781997-11-20 19:54:16 +000020
Barry Warsaw17c8e781997-11-20 19:54:16 +000021
Fred Drake4ef33291998-03-10 05:32:30 +000022\begin{classdesc}{Queue}{maxsize}
Barry Warsaw17c8e781997-11-20 19:54:16 +000023Constructor for the class. \var{maxsize} is an integer that sets the
24upperbound limit on the number of items that can be placed in the
25queue. Insertion will block once this size has been reached, until
26queue items are consumed. If \var{maxsize} is less than or equal to
27zero, the queue size is infinite.
Fred Drake4ef33291998-03-10 05:32:30 +000028\end{classdesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000029
Fred Drake4ef33291998-03-10 05:32:30 +000030\begin{excdesc}{Empty}
Guido van Rossumce67f061999-02-08 18:43:13 +000031Exception raised when non-blocking \method{get()} (or
32\method{get_nowait()}) is called on a \class{Queue} object which is
33empty or locked.
34\end{excdesc}
35
36\begin{excdesc}{Full}
37Exception raised when non-blocking \method{put()} (or
Fred Drake38e5d272000-04-03 20:13:55 +000038\method{put_nowait()}) is called on a \class{Queue} object which is
Guido van Rossumce67f061999-02-08 18:43:13 +000039full or locked.
Fred Drake4ef33291998-03-10 05:32:30 +000040\end{excdesc}
41
42\subsection{Queue Objects}
43\label{QueueObjects}
44
45Class \class{Queue} implements queue objects and has the methods
46described below. This class can be derived from in order to implement
47other queue organizations (e.g. stack) but the inheritable interface
48is not described here. See the source code for details. The public
49methods are:
50
Fred Drake8fe533e1998-03-27 05:27:08 +000051\begin{methoddesc}{qsize}{}
Guido van Rossumce67f061999-02-08 18:43:13 +000052Return the approximate size of the queue. Because of multithreading
Barry Warsaw17c8e781997-11-20 19:54:16 +000053semantics, this number is not reliable.
Fred Drake8fe533e1998-03-27 05:27:08 +000054\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000055
Fred Drake8fe533e1998-03-27 05:27:08 +000056\begin{methoddesc}{empty}{}
Martin v. Löwis77ac4292002-10-15 15:11:13 +000057Return \code{True} if the queue is empty, \code{False} otherwise.
58Becauseof multithreading semantics, this is not reliable.
Fred Drake8fe533e1998-03-27 05:27:08 +000059\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000060
Fred Drake8fe533e1998-03-27 05:27:08 +000061\begin{methoddesc}{full}{}
Martin v. Löwis77ac4292002-10-15 15:11:13 +000062Return \code{True} if the queue is full, \code{False} otherwise.
63Because of multithreading semantics, this is not reliable.
Fred Drake8fe533e1998-03-27 05:27:08 +000064\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000065
Martin v. Löwis77ac4292002-10-15 15:11:13 +000066\begin{methoddesc}{put}{item\optional{, block\optional{, timeout}}}
67Put \var{item} into the queue. If optional args \var{block} is true
68and \var{timeout} is None (the default), block if necessary until a
69free slot is available. If \var{timeout} is a positive number, it
70blocks at most \var{timeout} seconds and raises the \exception{Full}
71exception if no free slot was available within that time.
72Otherwise (\var{block} is false), put an item on the queue if a free
Guido van Rossumce67f061999-02-08 18:43:13 +000073slot is immediately available, else raise the \exception{Full}
Martin v. Löwis77ac4292002-10-15 15:11:13 +000074exception (\var{timeout} is ignored in that case).
75
76\versionadded[the timeout parameter]{2.3}
77
Fred Drake8fe533e1998-03-27 05:27:08 +000078\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000079
Guido van Rossumce67f061999-02-08 18:43:13 +000080\begin{methoddesc}{put_nowait}{item}
Martin v. Löwis77ac4292002-10-15 15:11:13 +000081Equivalent to \code{put(\var{item}, False)}.
Guido van Rossumce67f061999-02-08 18:43:13 +000082\end{methoddesc}
83
Martin v. Löwis77ac4292002-10-15 15:11:13 +000084\begin{methoddesc}{get}{\optional{block\optional{, timeout}}}
85Remove and return an item from the queue. If optional args
86\var{block} is true and \var{timeout} is None (the default),
87block if necessary until an item is available. If \var{timeout} is
88a positive number, it blocks at most \var{timeout} seconds and raises
89the \exception{Empty} exception if no item was available within that
90time. Otherwise (\var{block} is false), return an item if one is
91immediately available, else raise the \exception{Empty} exception
92(\var{timeout} is ignored in that case).
93
94\versionadded[the timeout parameter]{2.3}
95
Fred Drake8fe533e1998-03-27 05:27:08 +000096\end{methoddesc}
Barry Warsaw17c8e781997-11-20 19:54:16 +000097
Fred Drake8fe533e1998-03-27 05:27:08 +000098\begin{methoddesc}{get_nowait}{}
Martin v. Löwis77ac4292002-10-15 15:11:13 +000099Equivalent to \code{get(False)}.
Fred Drake8fe533e1998-03-27 05:27:08 +0000100\end{methoddesc}