Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b4a53d2

Browse filesBrowse files
committed
Change Enum to global about shutdown_state attr
Fixes bugs
1 parent 0075039 commit b4a53d2
Copy full SHA for b4a53d2

File tree

Expand file treeCollapse file tree

1 file changed

+29
-23
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+29
-23
lines changed

‎Lib/queue.py

Copy file name to clipboardExpand all lines: Lib/queue.py
+29-23Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ class ShutDown(Exception):
3030
'''Raised when put/get with shut-down queue.'''
3131

3232

33-
class _QueueState(enum.Enum):
34-
ALIVE = "alive"
35-
SHUTDOWN = "shutdown"
36-
SHUTDOWN_IMMEDIATE = "shutdown-immediate"
37-
33+
_queue_alive = "alive"
34+
_queue_shutdown = "shutdown"
35+
_queue_shutdown_immediate = "shutdown-immediate"
3836

3937
class Queue:
4038
'''Create a queue object with a given maximum size.
@@ -66,7 +64,7 @@ def __init__(self, maxsize=0):
6664
self.unfinished_tasks = 0
6765

6866
# Queue shut-down state
69-
self.shutdown_state = _QueueState.ALIVE
67+
self.shutdown_state = _queue_alive
7068

7169
def task_done(self):
7270
'''Indicate that a formerly enqueued task is complete.
@@ -83,6 +81,9 @@ def task_done(self):
8381
placed in the queue.
8482
'''
8583
with self.all_tasks_done:
84+
# here `self.all_task_done` uses `self.mutex`
85+
if self.shutdown_state == _queue_shutdown_immediate:
86+
raise ShutDown
8687
unfinished = self.unfinished_tasks - 1
8788
if unfinished <= 0:
8889
if unfinished < 0:
@@ -100,10 +101,13 @@ def join(self):
100101
When the count of unfinished tasks drops to zero, join() unblocks.
101102
'''
102103
with self.all_tasks_done:
104+
# here `self.all_task_done` uses `self.mutex`
105+
if self.shutdown_state == _queue_shutdown_immediate:
106+
raise ShutDown
103107
while self.unfinished_tasks:
104-
if self.shutdown_state is _QueueState.SHUTDOWN_IMMEDIATE:
105-
return
106108
self.all_tasks_done.wait()
109+
if self.shutdown_state == _queue_shutdown_immediate:
110+
raise ShutDown
107111

108112
def qsize(self):
109113
'''Return the approximate size of the queue (not reliable!).'''
@@ -146,17 +150,18 @@ def put(self, item, block=True, timeout=None):
146150
is immediately available, else raise the Full exception ('timeout'
147151
is ignored in that case).
148152
'''
149-
if self.shutdown_state is not _QueueState.ALIVE:
150-
raise ShutDown
151153
with self.not_full:
154+
# here `self.not_full` uses `self.mutex``
155+
if self.shutdown_state != _queue_alive:
156+
raise ShutDown
152157
if self.maxsize > 0:
153158
if not block:
154159
if self._qsize() >= self.maxsize:
155160
raise Full
156161
elif timeout is None:
157162
while self._qsize() >= self.maxsize:
158163
self.not_full.wait()
159-
if self.shutdown_state is not _QueueState.ALIVE:
164+
if self.shutdown_state != _queue_alive:
160165
raise ShutDown
161166
elif timeout < 0:
162167
raise ValueError("'timeout' must be a non-negative number")
@@ -167,7 +172,7 @@ def put(self, item, block=True, timeout=None):
167172
if remaining <= 0.0:
168173
raise Full
169174
self.not_full.wait(remaining)
170-
if self.shutdown_state is not _QueueState.ALIVE:
175+
if self.shutdown_state != _queue_alive:
171176
raise ShutDown
172177
self._put(item)
173178
self.unfinished_tasks += 1
@@ -184,35 +189,36 @@ def get(self, block=True, timeout=None):
184189
available, else raise the Empty exception ('timeout' is ignored
185190
in that case).
186191
'''
187-
if self.shutdown_state is _QueueState.SHUTDOWN_IMMEDIATE:
188-
raise ShutDown
189192
with self.not_empty:
193+
# here `self.not_empty` uses `self.mutex`
194+
if self.shutdown_state == _queue_shutdown_immediate:
195+
raise ShutDown
190196
if not block:
191197
if not self._qsize():
192-
if self.shutdown_state is not _QueueState.ALIVE:
198+
if self.shutdown_state != _queue_alive:
193199
raise ShutDown
194200
raise Empty
195201
elif timeout is None:
196202
while not self._qsize():
197-
if self.shutdown_state is not _QueueState.ALIVE:
203+
if self.shutdown_state != _queue_alive:
198204
raise ShutDown
199205
self.not_empty.wait()
200-
if self.shutdown_state is not _QueueState.ALIVE:
206+
if self.shutdown_state != _queue_alive:
201207
raise ShutDown
202208
elif timeout < 0:
203209
raise ValueError("'timeout' must be a non-negative number")
204210
else:
205211
endtime = time() + timeout
206212
while not self._qsize():
207-
if self.shutdown_state is not _QueueState.ALIVE:
213+
if self.shutdown_state != _queue_alive:
208214
raise ShutDown
209215
remaining = endtime - time()
210216
if remaining <= 0.0:
211217
raise Empty
212218
self.not_empty.wait(remaining)
213-
if self.shutdown_state is not _QueueState.ALIVE:
219+
if self.shutdown_state != _queue_alive:
214220
raise ShutDown
215-
if self.shutdown_state is _QueueState.SHUTDOWN_IMMEDIATE:
221+
if self.shutdown_state == _queue_shutdown_immediate:
216222
raise ShutDown
217223
item = self._get()
218224
self.not_full.notify()
@@ -244,19 +250,19 @@ def shutdown(self, immediate=False):
244250
and join() if 'immediate'. The ShutDown exception is raised.
245251
'''
246252
with self.mutex:
247-
if self.shutdown_state is _QueueState.SHUTDOWN_IMMEDIATE:
253+
if self.shutdown_state is _queue_shutdown_immediate:
248254
return
249255

250256
if immediate:
251-
self.shutdown_state = _QueueState.SHUTDOWN_IMMEDIATE
257+
self.shutdown_state = _queue_shutdown_immediate
252258
self.not_empty.notify_all()
253259
# set self.unfinished_tasks to 0
254260
# to break the loop in 'self.join()'
255261
# when quits from `wait()`
256262
self.unfinished_tasks = 0
257263
self.all_tasks_done.notify_all()
258264
else:
259-
self.shutdown_state = _QueueState.SHUTDOWN
265+
self.shutdown_state = _queue_shutdown
260266
self.not_full.notify_all()
261267

262268
# Override these methods to implement other queue organizations

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.