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 | |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 3 | from time import time as _time |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 4 | from collections import deque |
Martin v. Löwis | 77ac429 | 2002-10-15 15:11:13 +0000 | [diff] [blame] | 5 | |
Brett Cannon | b42bb5a | 2003-07-01 05:34:27 +0000 | [diff] [blame] | 6 | __all__ = ['Empty', 'Full', 'Queue'] |
| 7 | |
Tim Peters | b8c9417 | 2001-01-15 22:53:46 +0000 | [diff] [blame] | 8 | class Empty(Exception): |
| 9 | "Exception raised by Queue.get(block=0)/get_nowait()." |
| 10 | pass |
| 11 | |
| 12 | class Full(Exception): |
| 13 | "Exception raised by Queue.put(block=0)/put_nowait()." |
| 14 | pass |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 15 | |
| 16 | class Queue: |
Guido van Rossum | a41c691 | 1999-09-09 14:54:28 +0000 | [diff] [blame] | 17 | def __init__(self, maxsize=0): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 18 | """Initialize a queue object with a given maximum size. |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 20 | If maxsize is <= 0, the queue size is infinite. |
| 21 | """ |
Guido van Rossum | a093424 | 2002-12-30 22:36:09 +0000 | [diff] [blame] | 22 | try: |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 23 | import threading |
Guido van Rossum | a093424 | 2002-12-30 22:36:09 +0000 | [diff] [blame] | 24 | except ImportError: |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 25 | import dummy_threading as threading |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 26 | self._init(maxsize) |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 27 | # mutex must be held whenever the queue is mutating. All methods |
| 28 | # that acquire mutex must release it before returning. mutex |
| 29 | # is shared between the two conditions, so acquiring and |
| 30 | # releasing the conditions also acquires and releases mutex. |
| 31 | self.mutex = threading.Lock() |
| 32 | # Notify not_empty whenever an item is added to the queue; a |
| 33 | # thread waiting to get is notified then. |
| 34 | self.not_empty = threading.Condition(self.mutex) |
| 35 | # Notify not_full whenever an item is removed from the queue; |
| 36 | # a thread waiting to put is notified then. |
| 37 | self.not_full = threading.Condition(self.mutex) |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 38 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 39 | def qsize(self): |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 40 | """Return the approximate size of the queue (not reliable!).""" |
Guido van Rossum | 7e6d18c | 1998-04-29 14:29:32 +0000 | [diff] [blame] | 41 | self.mutex.acquire() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 42 | n = self._qsize() |
Guido van Rossum | 7e6d18c | 1998-04-29 14:29:32 +0000 | [diff] [blame] | 43 | self.mutex.release() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 44 | return n |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 45 | |
| 46 | def empty(self): |
Martin v. Löwis | 77ac429 | 2002-10-15 15:11:13 +0000 | [diff] [blame] | 47 | """Return True if the queue is empty, False otherwise (not reliable!).""" |
Guido van Rossum | 7e6d18c | 1998-04-29 14:29:32 +0000 | [diff] [blame] | 48 | self.mutex.acquire() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 49 | n = self._empty() |
Guido van Rossum | 7e6d18c | 1998-04-29 14:29:32 +0000 | [diff] [blame] | 50 | self.mutex.release() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 51 | return n |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 52 | |
| 53 | def full(self): |
Martin v. Löwis | 77ac429 | 2002-10-15 15:11:13 +0000 | [diff] [blame] | 54 | """Return True if the queue is full, False otherwise (not reliable!).""" |
Guido van Rossum | 7e6d18c | 1998-04-29 14:29:32 +0000 | [diff] [blame] | 55 | self.mutex.acquire() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 56 | n = self._full() |
Guido van Rossum | 7e6d18c | 1998-04-29 14:29:32 +0000 | [diff] [blame] | 57 | self.mutex.release() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 58 | return n |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 59 | |
Martin v. Löwis | 77ac429 | 2002-10-15 15:11:13 +0000 | [diff] [blame] | 60 | def put(self, item, block=True, timeout=None): |
Guido van Rossum | c09e6b1 | 1998-04-09 14:25:32 +0000 | [diff] [blame] | 61 | """Put an item into the queue. |
| 62 | |
Martin v. Löwis | 77ac429 | 2002-10-15 15:11:13 +0000 | [diff] [blame] | 63 | If optional args 'block' is true and 'timeout' is None (the default), |
| 64 | block if necessary until a free slot is available. If 'timeout' is |
| 65 | a positive number, it blocks at most 'timeout' seconds and raises |
| 66 | the Full exception if no free slot was available within that time. |
| 67 | Otherwise ('block' is false), put an item on the queue if a free slot |
| 68 | is immediately available, else raise the Full exception ('timeout' |
| 69 | is ignored in that case). |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 70 | """ |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 71 | self.not_full.acquire() |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 72 | try: |
Tim Peters | 71ed220 | 2004-07-12 01:20:32 +0000 | [diff] [blame] | 73 | if not block: |
| 74 | if self._full(): |
| 75 | raise Full |
| 76 | elif timeout is None: |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 77 | while self._full(): |
| 78 | self.not_full.wait() |
| 79 | else: |
| 80 | if timeout < 0: |
| 81 | raise ValueError("'timeout' must be a positive number") |
| 82 | endtime = _time() + timeout |
| 83 | while self._full(): |
| 84 | remaining = endtime - _time() |
Tim Peters | 71ed220 | 2004-07-12 01:20:32 +0000 | [diff] [blame] | 85 | if remaining <= 0.0: |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 86 | raise Full |
| 87 | self.not_full.wait(remaining) |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 88 | self._put(item) |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 89 | self.not_empty.notify() |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 90 | finally: |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 91 | self.not_full.release() |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 92 | |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 93 | def put_nowait(self, item): |
| 94 | """Put an item into the queue without blocking. |
Guido van Rossum | c09e6b1 | 1998-04-09 14:25:32 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 96 | Only enqueue the item if a free slot is immediately available. |
| 97 | Otherwise raise the Full exception. |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 98 | """ |
Tim Peters | 71ed220 | 2004-07-12 01:20:32 +0000 | [diff] [blame] | 99 | return self.put(item, False) |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 100 | |
Martin v. Löwis | 77ac429 | 2002-10-15 15:11:13 +0000 | [diff] [blame] | 101 | def get(self, block=True, timeout=None): |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 102 | """Remove and return an item from the queue. |
Guido van Rossum | c09e6b1 | 1998-04-09 14:25:32 +0000 | [diff] [blame] | 103 | |
Martin v. Löwis | 77ac429 | 2002-10-15 15:11:13 +0000 | [diff] [blame] | 104 | If optional args 'block' is true and 'timeout' is None (the default), |
| 105 | block if necessary until an item is available. If 'timeout' is |
| 106 | a positive number, it blocks at most 'timeout' seconds and raises |
| 107 | the Empty exception if no item was available within that time. |
| 108 | Otherwise ('block' is false), return an item if one is immediately |
| 109 | available, else raise the Empty exception ('timeout' is ignored |
| 110 | in that case). |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 111 | """ |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 112 | self.not_empty.acquire() |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 113 | try: |
Tim Peters | 71ed220 | 2004-07-12 01:20:32 +0000 | [diff] [blame] | 114 | if not block: |
| 115 | if self._empty(): |
| 116 | raise Empty |
| 117 | elif timeout is None: |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 118 | while self._empty(): |
| 119 | self.not_empty.wait() |
| 120 | else: |
| 121 | if timeout < 0: |
| 122 | raise ValueError("'timeout' must be a positive number") |
| 123 | endtime = _time() + timeout |
| 124 | while self._empty(): |
| 125 | remaining = endtime - _time() |
Tim Peters | 71ed220 | 2004-07-12 01:20:32 +0000 | [diff] [blame] | 126 | if remaining <= 0.0: |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 127 | raise Empty |
| 128 | self.not_empty.wait(remaining) |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 129 | item = self._get() |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 130 | self.not_full.notify() |
| 131 | return item |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 132 | finally: |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 133 | self.not_empty.release() |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 134 | |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 135 | def get_nowait(self): |
| 136 | """Remove and return an item from the queue without blocking. |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 137 | |
Martin v. Löwis | 77ac429 | 2002-10-15 15:11:13 +0000 | [diff] [blame] | 138 | Only get an item if one is immediately available. Otherwise |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 139 | raise the Empty exception. |
| 140 | """ |
Tim Peters | 71ed220 | 2004-07-12 01:20:32 +0000 | [diff] [blame] | 141 | return self.get(False) |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 142 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 143 | # Override these methods to implement other queue organizations |
| 144 | # (e.g. stack or priority queue). |
| 145 | # These will only be called with appropriate locks held |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 146 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 147 | # Initialize the queue representation |
| 148 | def _init(self, maxsize): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 149 | self.maxsize = maxsize |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 150 | self.queue = deque() |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 151 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 152 | def _qsize(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 153 | return len(self.queue) |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 154 | |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 155 | # Check whether the queue is empty |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 156 | def _empty(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 157 | return not self.queue |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 158 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 159 | # Check whether the queue is full |
| 160 | def _full(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 161 | return self.maxsize > 0 and len(self.queue) == self.maxsize |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 162 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 163 | # Put a new item in the queue |
| 164 | def _put(self, item): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 165 | self.queue.append(item) |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 166 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 167 | # Get an item from the queue |
| 168 | def _get(self): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 169 | return self.queue.popleft() |