1 """A multi-producer, multi-consumer queue."""
3 from time
import time
as _time
4 from collections
import deque
6 __all__
= ['Empty', 'Full', 'Queue']
8 class Empty(Exception):
9 "Exception raised by Queue.get(block=0)/get_nowait()."
12 class Full(Exception):
13 "Exception raised by Queue.put(block=0)/put_nowait()."
17 def __init__(self
, maxsize
=0):
18 """Initialize a queue object with a given maximum size.
20 If maxsize is <= 0, the queue size is infinite.
25 import dummy_threading
as threading
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
)
40 """Return the approximate size of the queue (not reliable!)."""
47 """Return True if the queue is empty, False otherwise (not reliable!)."""
54 """Return True if the queue is full, False otherwise (not reliable!)."""
60 def put(self
, item
, block
=True, timeout
=None):
61 """Put an item into the queue.
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).
71 self
.not_full
.acquire()
81 raise ValueError("'timeout' must be a positive number")
82 endtime
= _time() + timeout
84 remaining
= endtime
- _time()
87 self
.not_full
.wait(remaining
)
89 self
.not_empty
.notify()
91 self
.not_full
.release()
93 def put_nowait(self
, item
):
94 """Put an item into the queue without blocking.
96 Only enqueue the item if a free slot is immediately available.
97 Otherwise raise the Full exception.
99 return self
.put(item
, False)
101 def get(self
, block
=True, timeout
=None):
102 """Remove and return an item from the queue.
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
112 self
.not_empty
.acquire()
117 elif timeout
is None:
119 self
.not_empty
.wait()
122 raise ValueError("'timeout' must be a positive number")
123 endtime
= _time() + timeout
125 remaining
= endtime
- _time()
128 self
.not_empty
.wait(remaining
)
130 self
.not_full
.notify()
133 self
.not_empty
.release()
135 def get_nowait(self
):
136 """Remove and return an item from the queue without blocking.
138 Only get an item if one is immediately available. Otherwise
139 raise the Empty exception.
141 return self
.get(False)
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
147 # Initialize the queue representation
148 def _init(self
, maxsize
):
149 self
.maxsize
= maxsize
153 return len(self
.queue
)
155 # Check whether the queue is empty
157 return not self
.queue
159 # Check whether the queue is full
161 return self
.maxsize
> 0 and len(self
.queue
) == self
.maxsize
163 # Put a new item in the queue
164 def _put(self
, item
):
165 self
.queue
.append(item
)
167 # Get an item from the queue
169 return self
.queue
.popleft()