Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 1 | # A multi-producer, multi-consumer queue. |
| 2 | |
| 3 | Empty = 'Queue.Empty' # Exception raised by get_nowait() |
| 4 | |
| 5 | class Queue: |
| 6 | |
| 7 | # Initialize a queue object with a given maximum size |
| 8 | # (If maxsize is <= 0, the maximum size is infinite) |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 9 | def __init__(self, maxsize): |
Guido van Rossum | dd8cb44 | 1993-12-29 15:33:08 +0000 | [diff] [blame] | 10 | import thread |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 11 | self._init(maxsize) |
| 12 | self.mutex = thread.allocate_lock() |
| 13 | self.esema = thread.allocate_lock() |
| 14 | self.esema.acquire_lock() |
| 15 | self.fsema = thread.allocate_lock() |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 16 | |
| 17 | # Get an approximation of the queue size (not reliable!) |
| 18 | def qsize(self): |
| 19 | self.mutex.acquire_lock() |
| 20 | n = self._qsize() |
| 21 | self.mutex.release_lock() |
| 22 | return n |
| 23 | |
| 24 | # Check if the queue is empty (not reliable!) |
| 25 | def empty(self): |
| 26 | self.mutex.acquire_lock() |
| 27 | n = self._empty() |
| 28 | self.mutex.release_lock() |
| 29 | return n |
| 30 | |
| 31 | # Check if the queue is full (not reliable!) |
| 32 | def full(self): |
| 33 | self.mutex.acquire_lock() |
| 34 | n = self._full() |
| 35 | self.mutex.release_lock() |
| 36 | return n |
| 37 | |
| 38 | # Put a new item into the queue |
| 39 | def put(self, item): |
| 40 | self.fsema.acquire_lock() |
| 41 | self.mutex.acquire_lock() |
| 42 | was_empty = self._empty() |
| 43 | self._put(item) |
| 44 | if was_empty: |
| 45 | self.esema.release_lock() |
| 46 | if not self._full(): |
| 47 | self.fsema.release_lock() |
| 48 | self.mutex.release_lock() |
| 49 | |
| 50 | # Get an item from the queue, |
| 51 | # blocking if necessary until one is available |
| 52 | def get(self): |
| 53 | self.esema.acquire_lock() |
| 54 | self.mutex.acquire_lock() |
| 55 | was_full = self._full() |
| 56 | item = self._get() |
| 57 | if was_full: |
| 58 | self.fsema.release_lock() |
| 59 | if not self._empty(): |
| 60 | self.esema.release_lock() |
| 61 | self.mutex.release_lock() |
| 62 | return item |
| 63 | |
| 64 | # Get an item from the queue if one is immediately available, |
| 65 | # raise Empty if the queue is empty or temporarily unavailable |
| 66 | def get_nowait(self): |
| 67 | locked = self.esema.acquire_lock(0) |
| 68 | self.mutex.acquire_lock() |
| 69 | if self._empty(): |
| 70 | # The queue is empyt -- we can't have esema |
| 71 | self.mutex.release_lock() |
| 72 | raise Empty |
| 73 | if not locked: |
| 74 | locked = self.esema.acquire_lock(0) |
| 75 | if not locked: |
| 76 | # Somebody else has esema |
| 77 | # but we have mutex -- |
| 78 | # go out of their way |
| 79 | self.mutex.release_lock() |
| 80 | raise Empty |
| 81 | was_full = self._full() |
| 82 | item = self._get() |
| 83 | if was_full: |
| 84 | self.fsema.release_lock() |
| 85 | if not self._empty(): |
| 86 | self.esema.release_lock() |
| 87 | self.mutex.release_lock() |
| 88 | return item |
| 89 | |
| 90 | # XXX Need to define put_nowait() as well. |
| 91 | |
| 92 | |
| 93 | # Override these methods to implement other queue organizations |
| 94 | # (e.g. stack or priority queue). |
| 95 | # These will only be called with appropriate locks held |
| 96 | |
| 97 | # Initialize the queue representation |
| 98 | def _init(self, maxsize): |
| 99 | self.maxsize = maxsize |
| 100 | self.queue = [] |
| 101 | |
| 102 | def _qsize(self): |
| 103 | return len(self.queue) |
| 104 | |
| 105 | # Check wheter the queue is empty |
| 106 | def _empty(self): |
| 107 | return not self.queue |
| 108 | |
| 109 | # Check whether the queue is full |
| 110 | def _full(self): |
| 111 | return self.maxsize > 0 and len(self.queue) == self.maxsize |
| 112 | |
| 113 | # Put a new item in the queue |
| 114 | def _put(self, item): |
| 115 | self.queue.append(item) |
| 116 | |
| 117 | # Get an item from the queue |
| 118 | def _get(self): |
| 119 | item = self.queue[0] |
| 120 | del self.queue[0] |
| 121 | return item |