Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 1 | """A multi-producer, multi-consumer queue.""" |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 2 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 3 | __all__ = ["Queue","Empty","Full"] |
| 4 | |
Tim Peters | b8c9417 | 2001-01-15 22:53:46 +0000 | [diff] [blame] | 5 | class Empty(Exception): |
| 6 | "Exception raised by Queue.get(block=0)/get_nowait()." |
| 7 | pass |
| 8 | |
| 9 | class Full(Exception): |
| 10 | "Exception raised by Queue.put(block=0)/put_nowait()." |
| 11 | pass |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 12 | |
| 13 | class Queue: |
Guido van Rossum | a41c691 | 1999-09-09 14:54:28 +0000 | [diff] [blame] | 14 | def __init__(self, maxsize=0): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 15 | """Initialize a queue object with a given maximum size. |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 17 | If maxsize is <= 0, the queue size is infinite. |
| 18 | """ |
| 19 | import thread |
| 20 | self._init(maxsize) |
| 21 | self.mutex = thread.allocate_lock() |
| 22 | self.esema = thread.allocate_lock() |
Guido van Rossum | 7e6d18c | 1998-04-29 14:29:32 +0000 | [diff] [blame] | 23 | self.esema.acquire() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 24 | self.fsema = thread.allocate_lock() |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 25 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 26 | def qsize(self): |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 27 | """Return the approximate size of the queue (not reliable!).""" |
Guido van Rossum | 7e6d18c | 1998-04-29 14:29:32 +0000 | [diff] [blame] | 28 | self.mutex.acquire() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 29 | n = self._qsize() |
Guido van Rossum | 7e6d18c | 1998-04-29 14:29:32 +0000 | [diff] [blame] | 30 | self.mutex.release() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 31 | return n |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 32 | |
| 33 | def empty(self): |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 34 | """Return 1 if the queue is empty, 0 otherwise (not reliable!).""" |
Guido van Rossum | 7e6d18c | 1998-04-29 14:29:32 +0000 | [diff] [blame] | 35 | self.mutex.acquire() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 36 | n = self._empty() |
Guido van Rossum | 7e6d18c | 1998-04-29 14:29:32 +0000 | [diff] [blame] | 37 | self.mutex.release() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 38 | return n |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 39 | |
| 40 | def full(self): |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 41 | """Return 1 if the queue is full, 0 otherwise (not reliable!).""" |
Guido van Rossum | 7e6d18c | 1998-04-29 14:29:32 +0000 | [diff] [blame] | 42 | self.mutex.acquire() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 43 | n = self._full() |
Guido van Rossum | 7e6d18c | 1998-04-29 14:29:32 +0000 | [diff] [blame] | 44 | self.mutex.release() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 45 | return n |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 47 | def put(self, item, block=1): |
Guido van Rossum | c09e6b1 | 1998-04-09 14:25:32 +0000 | [diff] [blame] | 48 | """Put an item into the queue. |
| 49 | |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 50 | If optional arg 'block' is 1 (the default), block if |
| 51 | necessary until a free slot is available. Otherwise (block |
| 52 | is 0), put an item on the queue if a free slot is immediately |
| 53 | available, else raise the Full exception. |
| 54 | """ |
| 55 | if block: |
| 56 | self.fsema.acquire() |
| 57 | elif not self.fsema.acquire(0): |
| 58 | raise Full |
Guido van Rossum | 7e6d18c | 1998-04-29 14:29:32 +0000 | [diff] [blame] | 59 | self.mutex.acquire() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 60 | was_empty = self._empty() |
| 61 | self._put(item) |
| 62 | if was_empty: |
Guido van Rossum | 7e6d18c | 1998-04-29 14:29:32 +0000 | [diff] [blame] | 63 | self.esema.release() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 64 | if not self._full(): |
Guido van Rossum | 7e6d18c | 1998-04-29 14:29:32 +0000 | [diff] [blame] | 65 | self.fsema.release() |
| 66 | self.mutex.release() |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 68 | def put_nowait(self, item): |
| 69 | """Put an item into the queue without blocking. |
Guido van Rossum | c09e6b1 | 1998-04-09 14:25:32 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 71 | Only enqueue the item if a free slot is immediately available. |
| 72 | Otherwise raise the Full exception. |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 73 | """ |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 74 | return self.put(item, 0) |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 76 | def get(self, block=1): |
| 77 | """Remove and return an item from the queue. |
Guido van Rossum | c09e6b1 | 1998-04-09 14:25:32 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 79 | If optional arg 'block' is 1 (the default), block if |
| 80 | necessary until an item is available. Otherwise (block is 0), |
| 81 | return an item if one is immediately available, else raise the |
| 82 | Empty exception. |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 83 | """ |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 84 | if block: |
| 85 | self.esema.acquire() |
| 86 | elif not self.esema.acquire(0): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 87 | raise Empty |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 88 | self.mutex.acquire() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 89 | was_full = self._full() |
| 90 | item = self._get() |
| 91 | if was_full: |
Guido van Rossum | 7e6d18c | 1998-04-29 14:29:32 +0000 | [diff] [blame] | 92 | self.fsema.release() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 93 | if not self._empty(): |
Guido van Rossum | 7e6d18c | 1998-04-29 14:29:32 +0000 | [diff] [blame] | 94 | self.esema.release() |
| 95 | self.mutex.release() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 96 | return item |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 98 | def get_nowait(self): |
| 99 | """Remove and return an item from the queue without blocking. |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 100 | |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 101 | Only get an item if one is immediately available. Otherwise |
| 102 | raise the Empty exception. |
| 103 | """ |
| 104 | return self.get(0) |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 105 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 106 | # Override these methods to implement other queue organizations |
| 107 | # (e.g. stack or priority queue). |
| 108 | # These will only be called with appropriate locks held |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 109 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 110 | # Initialize the queue representation |
| 111 | def _init(self, maxsize): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 112 | self.maxsize = maxsize |
| 113 | self.queue = [] |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 114 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 115 | def _qsize(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 116 | return len(self.queue) |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 117 | |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 118 | # Check whether the queue is empty |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 119 | def _empty(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 120 | return not self.queue |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 121 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 122 | # Check whether the queue is full |
| 123 | def _full(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 124 | return self.maxsize > 0 and len(self.queue) == self.maxsize |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 125 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 126 | # Put a new item in the queue |
| 127 | def _put(self, item): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 128 | self.queue.append(item) |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 129 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 130 | # Get an item from the queue |
| 131 | def _get(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 132 | item = self.queue[0] |
| 133 | del self.queue[0] |
| 134 | return item |