Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame] | 1 | \section{Standard Module \module{Queue}} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 2 | \stmodindex{Queue} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 3 | \label{module-Queue} |
| 4 | |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 5 | |
Fred Drake | 4ef3329 | 1998-03-10 05:32:30 +0000 | [diff] [blame] | 6 | The \module{Queue} module implements a multi-producer, multi-consumer |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 7 | FIFO queue. It is especially useful in threads programming when |
| 8 | information must be exchanged safely between multiple threads. The |
Fred Drake | 4ef3329 | 1998-03-10 05:32:30 +0000 | [diff] [blame] | 9 | \class{Queue} class in this module implements all the required locking |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 10 | semantics. It depends on the availability of thread support in |
| 11 | Python. |
| 12 | |
Fred Drake | 4ef3329 | 1998-03-10 05:32:30 +0000 | [diff] [blame] | 13 | The \module{Queue} module defines the following class and exception: |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 14 | |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 15 | |
Fred Drake | 4ef3329 | 1998-03-10 05:32:30 +0000 | [diff] [blame] | 16 | \begin{classdesc}{Queue}{maxsize} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 17 | Constructor for the class. \var{maxsize} is an integer that sets the |
| 18 | upperbound limit on the number of items that can be placed in the |
| 19 | queue. Insertion will block once this size has been reached, until |
| 20 | queue items are consumed. If \var{maxsize} is less than or equal to |
| 21 | zero, the queue size is infinite. |
Fred Drake | 4ef3329 | 1998-03-10 05:32:30 +0000 | [diff] [blame] | 22 | \end{classdesc} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 23 | |
Fred Drake | 4ef3329 | 1998-03-10 05:32:30 +0000 | [diff] [blame] | 24 | \begin{excdesc}{Empty} |
| 25 | Exception raised when non-blocking get (e.g. \method{get_nowait()}) is |
| 26 | called on a \class{Queue} object which is empty, or for which the |
| 27 | emptyiness cannot be determined (i.e. because the appropriate locks |
| 28 | cannot be acquired). |
| 29 | \end{excdesc} |
| 30 | |
| 31 | \subsection{Queue Objects} |
| 32 | \label{QueueObjects} |
| 33 | |
| 34 | Class \class{Queue} implements queue objects and has the methods |
| 35 | described below. This class can be derived from in order to implement |
| 36 | other queue organizations (e.g. stack) but the inheritable interface |
| 37 | is not described here. See the source code for details. The public |
| 38 | methods are: |
| 39 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 40 | \begin{methoddesc}{qsize}{} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 41 | Returns the approximate size of the queue. Because of multithreading |
| 42 | semantics, this number is not reliable. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 43 | \end{methoddesc} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 44 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 45 | \begin{methoddesc}{empty}{} |
Fred Drake | 4ef3329 | 1998-03-10 05:32:30 +0000 | [diff] [blame] | 46 | Returns \code{1} if the queue is empty, \code{0} otherwise. Because |
| 47 | of multithreading semantics, this is not reliable. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 48 | \end{methoddesc} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 49 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 50 | \begin{methoddesc}{full}{} |
Fred Drake | 4ef3329 | 1998-03-10 05:32:30 +0000 | [diff] [blame] | 51 | Returns \code{1} if the queue is full, \code{0} otherwise. Because of |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 52 | multithreading semantics, this is not reliable. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 53 | \end{methoddesc} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 54 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 55 | \begin{methoddesc}{put}{item} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 56 | Puts \var{item} into the queue. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 57 | \end{methoddesc} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 58 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 59 | \begin{methoddesc}{get}{} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 60 | Gets and returns an item from the queue, blocking if necessary until |
| 61 | one is available. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 62 | \end{methoddesc} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 63 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 64 | \begin{methoddesc}{get_nowait}{} |
Barry Warsaw | 17c8e78 | 1997-11-20 19:54:16 +0000 | [diff] [blame] | 65 | Gets and returns an item from the queue if one is immediately |
Fred Drake | 4ef3329 | 1998-03-10 05:32:30 +0000 | [diff] [blame] | 66 | available. Raises an \exception{Empty} exception if the queue is |
| 67 | empty or if the queue's emptiness cannot be determined. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 68 | \end{methoddesc} |