1 """A multi-producer, multi-consumer queue."""
3 from time
import time
as _time
, sleep
as _sleep
5 class Empty(Exception):
6 "Exception raised by Queue.get(block=0)/get_nowait()."
10 "Exception raised by Queue.put(block=0)/put_nowait()."
14 def __init__(self
, maxsize
=0):
15 """Initialize a queue object with a given maximum size.
17 If maxsize is <= 0, the queue size is infinite.
22 import dummy_thread
as thread
24 self
.mutex
= thread
.allocate_lock()
25 self
.esema
= thread
.allocate_lock()
27 self
.fsema
= thread
.allocate_lock()
30 """Return the approximate size of the queue (not reliable!)."""
37 """Return True if the queue is empty, False otherwise (not reliable!)."""
44 """Return True if the queue is full, False otherwise (not reliable!)."""
50 def put(self
, item
, block
=True, timeout
=None):
51 """Put an item into the queue.
53 If optional args 'block' is true and 'timeout' is None (the default),
54 block if necessary until a free slot is available. If 'timeout' is
55 a positive number, it blocks at most 'timeout' seconds and raises
56 the Full exception if no free slot was available within that time.
57 Otherwise ('block' is false), put an item on the queue if a free slot
58 is immediately available, else raise the Full exception ('timeout'
59 is ignored in that case).
63 # blocking, w/o timeout, i.e. forever
66 # waiting max. 'timeout' seconds.
67 # this code snipped is from threading.py: _Event.wait():
68 # Balancing act: We can't afford a pure busy loop, so we
69 # have to sleep; but if we sleep the whole timeout time,
70 # we'll be unresponsive. The scheme here sleeps very
71 # little at first, longer as time goes on, but never longer
72 # than 20 times per second (or the timeout time remaining).
73 delay
= 0.0005 # 500 us -> initial delay of 1 ms
74 endtime
= _time() + timeout
76 if self
.fsema
.acquire(0):
78 remaining
= endtime
- _time()
79 if remaining
<= 0: #time is over and no slot was free
81 delay
= min(delay
* 2, remaining
, .05)
82 _sleep(delay
) #reduce CPU usage by using a sleep
84 raise ValueError("'timeout' must be a positive number")
85 elif not self
.fsema
.acquire(0):
90 was_empty
= self
._empty
()
92 # If we fail before here, the empty state has
93 # not changed, so we can skip the release of esema
96 # If we fail before here, the queue can not be full, so
97 # release_full_sema remains True
98 release_fsema
= not self
._full
()
100 # Catching system level exceptions here (RecursionDepth,
101 # OutOfMemory, etc) - so do as little as possible in terms
107 def put_nowait(self
, item
):
108 """Put an item into the queue without blocking.
110 Only enqueue the item if a free slot is immediately available.
111 Otherwise raise the Full exception.
113 return self
.put(item
, False)
115 def get(self
, block
=True, timeout
=None):
116 """Remove and return an item from the queue.
118 If optional args 'block' is true and 'timeout' is None (the default),
119 block if necessary until an item is available. If 'timeout' is
120 a positive number, it blocks at most 'timeout' seconds and raises
121 the Empty exception if no item was available within that time.
122 Otherwise ('block' is false), return an item if one is immediately
123 available, else raise the Empty exception ('timeout' is ignored
128 # blocking, w/o timeout, i.e. forever
131 # waiting max. 'timeout' seconds.
132 # this code snipped is from threading.py: _Event.wait():
133 # Balancing act: We can't afford a pure busy loop, so we
134 # have to sleep; but if we sleep the whole timeout time,
135 # we'll be unresponsive. The scheme here sleeps very
136 # little at first, longer as time goes on, but never longer
137 # than 20 times per second (or the timeout time remaining).
138 delay
= 0.0005 # 500 us -> initial delay of 1 ms
139 endtime
= _time() + timeout
141 if self
.esema
.acquire(0):
143 remaining
= endtime
- _time()
144 if remaining
<= 0: #time is over and no element arrived
146 delay
= min(delay
* 2, remaining
, .05)
147 _sleep(delay
) #reduce CPU usage by using a sleep
149 raise ValueError("'timeout' must be a positive number")
150 elif not self
.esema
.acquire(0):
155 was_full
= self
._full
()
157 # If we fail before here, the full state has
158 # not changed, so we can skip the release of fsema
161 # Failure means empty state also unchanged - release_esema
163 release_esema
= not self
._empty
()
170 def get_nowait(self
):
171 """Remove and return an item from the queue without blocking.
173 Only get an item if one is immediately available. Otherwise
174 raise the Empty exception.
176 return self
.get(False)
178 # Override these methods to implement other queue organizations
179 # (e.g. stack or priority queue).
180 # These will only be called with appropriate locks held
182 # Initialize the queue representation
183 def _init(self
, maxsize
):
184 self
.maxsize
= maxsize
188 return len(self
.queue
)
190 # Check whether the queue is empty
192 return not self
.queue
194 # Check whether the queue is full
196 return self
.maxsize
> 0 and len(self
.queue
) == self
.maxsize
198 # Put a new item in the queue
199 def _put(self
, item
):
200 self
.queue
.append(item
)
202 # Get an item from the queue
204 return self
.queue
.pop(0)