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