Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{Queue} --- |
Fred Drake | f8ca7d8 | 2000-10-10 17:03:45 +0000 | [diff] [blame] | 2 | A synchronized queue class} |
| 3 | |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{Queue} |
Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 5 | \modulesynopsis{A synchronized queue class.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 6 | |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 7 | |
Fred Drake | 4ef3329 | 1998-03-10 05:32:30 +0000 | [diff] [blame] | 8 | The \module{Queue} module implements a multi-producer, multi-consumer |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 9 | FIFO queue. It is especially useful in threads programming when |
| 10 | information must be exchanged safely between multiple threads. The |
Fred Drake | 4ef3329 | 1998-03-10 05:32:30 +0000 | [diff] [blame] | 11 | \class{Queue} class in this module implements all the required locking |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 12 | semantics. It depends on the availability of thread support in |
| 13 | Python. |
| 14 | |
Skip Montanaro | 763805d | 2002-06-26 05:22:08 +0000 | [diff] [blame] | 15 | \begin{seealso} |
| 16 | \seemodule{bisect}{PriorityQueue example using the Queue class} |
| 17 | \end{seealso} |
| 18 | |
Fred Drake | 4ef3329 | 1998-03-10 05:32:30 +0000 | [diff] [blame] | 19 | The \module{Queue} module defines the following class and exception: |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 20 | |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 21 | |
Fred Drake | 4ef3329 | 1998-03-10 05:32:30 +0000 | [diff] [blame] | 22 | \begin{classdesc}{Queue}{maxsize} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 23 | Constructor for the class. \var{maxsize} is an integer that sets the |
| 24 | upperbound limit on the number of items that can be placed in the |
| 25 | queue. Insertion will block once this size has been reached, until |
| 26 | queue items are consumed. If \var{maxsize} is less than or equal to |
| 27 | zero, the queue size is infinite. |
Fred Drake | 4ef3329 | 1998-03-10 05:32:30 +0000 | [diff] [blame] | 28 | \end{classdesc} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 29 | |
Fred Drake | 4ef3329 | 1998-03-10 05:32:30 +0000 | [diff] [blame] | 30 | \begin{excdesc}{Empty} |
Guido van Rossum | ce67f06 | 1999-02-08 18:43:13 +0000 | [diff] [blame] | 31 | Exception raised when non-blocking \method{get()} (or |
| 32 | \method{get_nowait()}) is called on a \class{Queue} object which is |
| 33 | empty or locked. |
| 34 | \end{excdesc} |
| 35 | |
| 36 | \begin{excdesc}{Full} |
| 37 | Exception raised when non-blocking \method{put()} (or |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 38 | \method{put_nowait()}) is called on a \class{Queue} object which is |
Guido van Rossum | ce67f06 | 1999-02-08 18:43:13 +0000 | [diff] [blame] | 39 | full or locked. |
Fred Drake | 4ef3329 | 1998-03-10 05:32:30 +0000 | [diff] [blame] | 40 | \end{excdesc} |
| 41 | |
| 42 | \subsection{Queue Objects} |
| 43 | \label{QueueObjects} |
| 44 | |
| 45 | Class \class{Queue} implements queue objects and has the methods |
| 46 | described below. This class can be derived from in order to implement |
| 47 | other queue organizations (e.g. stack) but the inheritable interface |
| 48 | is not described here. See the source code for details. The public |
| 49 | methods are: |
| 50 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 51 | \begin{methoddesc}{qsize}{} |
Guido van Rossum | ce67f06 | 1999-02-08 18:43:13 +0000 | [diff] [blame] | 52 | Return the approximate size of the queue. Because of multithreading |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 53 | semantics, this number is not reliable. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 54 | \end{methoddesc} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 55 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 56 | \begin{methoddesc}{empty}{} |
Guido van Rossum | ce67f06 | 1999-02-08 18:43:13 +0000 | [diff] [blame] | 57 | Return \code{1} if the queue is empty, \code{0} otherwise. Because |
Fred Drake | 4ef3329 | 1998-03-10 05:32:30 +0000 | [diff] [blame] | 58 | of multithreading semantics, this is not reliable. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 59 | \end{methoddesc} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 60 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 61 | \begin{methoddesc}{full}{} |
Guido van Rossum | ce67f06 | 1999-02-08 18:43:13 +0000 | [diff] [blame] | 62 | Return \code{1} if the queue is full, \code{0} otherwise. Because of |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 63 | multithreading semantics, this is not reliable. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 64 | \end{methoddesc} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | ce67f06 | 1999-02-08 18:43:13 +0000 | [diff] [blame] | 66 | \begin{methoddesc}{put}{item\optional{, block}} |
| 67 | Put \var{item} into the queue. If optional argument \var{block} is 1 |
| 68 | (the default), block if necessary until a free slot is available. |
| 69 | Otherwise (\var{block} is 0), put \var{item} on the queue if a free |
| 70 | slot is immediately available, else raise the \exception{Full} |
| 71 | exception. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 72 | \end{methoddesc} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | ce67f06 | 1999-02-08 18:43:13 +0000 | [diff] [blame] | 74 | \begin{methoddesc}{put_nowait}{item} |
| 75 | Equivalent to \code{put(\var{item}, 0)}. |
| 76 | \end{methoddesc} |
| 77 | |
| 78 | \begin{methoddesc}{get}{\optional{block}} |
| 79 | Remove and return an item from the queue. If optional argument |
| 80 | \var{block} is 1 (the default), block if necessary until an item is |
| 81 | available. Otherwise (\var{block} is 0), return an item if one is |
| 82 | immediately available, else raise the |
| 83 | \exception{Empty} exception. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 84 | \end{methoddesc} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 85 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 86 | \begin{methoddesc}{get_nowait}{} |
Guido van Rossum | ce67f06 | 1999-02-08 18:43:13 +0000 | [diff] [blame] | 87 | Equivalent to \code{get(0)}. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 88 | \end{methoddesc} |