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 5239306

Browse filesBrowse files
committed
replace global state variable with an enum _QueueState
1 parent dd22c6b commit 5239306
Copy full SHA for 5239306

File tree

Expand file treeCollapse file tree

1 file changed

+21
-17
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+21
-17
lines changed

‎Lib/queue.py

Copy file name to clipboardExpand all lines: Lib/queue.py
+21-17Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'''A multi-producer, multi-consumer queue.'''
22

3+
import enum
34
import threading
45
import types
56
from collections import deque
@@ -29,9 +30,12 @@ class ShutDown(Exception):
2930
'''Raised when put/get with shut-down queue.'''
3031

3132

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

3640

3741
class Queue:
@@ -64,7 +68,7 @@ def __init__(self, maxsize=0):
6468
self.unfinished_tasks = 0
6569

6670
# Queue shut-down state
67-
self.shutdown_state = _queue_alive
71+
self.shutdown_state = _QueueState.ALIVE
6872

6973
def task_done(self):
7074
'''Indicate that a formerly enqueued task is complete.
@@ -99,7 +103,7 @@ def join(self):
99103
'''
100104
with self.all_tasks_done:
101105
while self.unfinished_tasks:
102-
if self.shutdown_state == _queue_shutdown_immediate:
106+
if self.shutdown_state is _QueueState.SHUTDOWN_IMMEDIATE:
103107
return
104108
self.all_tasks_done.wait()
105109

@@ -144,7 +148,7 @@ def put(self, item, block=True, timeout=None):
144148
is immediately available, else raise the Full exception ('timeout'
145149
is ignored in that case).
146150
'''
147-
if self.shutdown_state != _queue_alive:
151+
if self.shutdown_state is not _QueueState.ALIVE:
148152
raise ShutDown
149153
with self.not_full:
150154
if self.maxsize > 0:
@@ -154,7 +158,7 @@ def put(self, item, block=True, timeout=None):
154158
elif timeout is None:
155159
while self._qsize() >= self.maxsize:
156160
self.not_full.wait()
157-
if self.shutdown_state != _queue_alive:
161+
if self.shutdown_state is not _QueueState.ALIVE:
158162
raise ShutDown
159163
elif timeout < 0:
160164
raise ValueError("'timeout' must be a non-negative number")
@@ -165,7 +169,7 @@ def put(self, item, block=True, timeout=None):
165169
if remaining <= 0.0:
166170
raise Full
167171
self.not_full.wait(remaining)
168-
if self.shutdown_state != _queue_alive:
172+
if self.shutdown_state is not _QueueState.ALIVE:
169173
raise ShutDown
170174
self._put(item)
171175
self.unfinished_tasks += 1
@@ -182,35 +186,35 @@ def get(self, block=True, timeout=None):
182186
available, else raise the Empty exception ('timeout' is ignored
183187
in that case).
184188
'''
185-
if self.shutdown_state == _queue_shutdown_immediate:
189+
if self.shutdown_state is _QueueState.SHUTDOWN_IMMEDIATE:
186190
raise ShutDown
187191
with self.not_empty:
188192
if not block:
189193
if not self._qsize():
190-
if self.shutdown_state != _queue_alive:
194+
if self.shutdown_state is not _QueueState.ALIVE:
191195
raise ShutDown
192196
raise Empty
193197
elif timeout is None:
194198
while not self._qsize():
195-
if self.shutdown_state != _queue_alive:
199+
if self.shutdown_state is not _QueueState.ALIVE:
196200
raise ShutDown
197201
self.not_empty.wait()
198-
if self.shutdown_state != _queue_alive:
202+
if self.shutdown_state is not _QueueState.ALIVE:
199203
raise ShutDown
200204
elif timeout < 0:
201205
raise ValueError("'timeout' must be a non-negative number")
202206
else:
203207
endtime = time() + timeout
204208
while not self._qsize():
205-
if self.shutdown_state != _queue_alive:
209+
if self.shutdown_state is not _QueueState.ALIVE:
206210
raise ShutDown
207211
remaining = endtime - time()
208212
if remaining <= 0.0:
209213
raise Empty
210214
self.not_empty.wait(remaining)
211-
if self.shutdown_state != _queue_alive:
215+
if self.shutdown_state is not _QueueState.ALIVE:
212216
raise ShutDown
213-
if self.shutdown_state == _queue_shutdown_immediate:
217+
if self.shutdown_state is _QueueState.SHUTDOWN_IMMEDIATE:
214218
raise ShutDown
215219
item = self._get()
216220
self.not_full.notify()
@@ -243,15 +247,15 @@ def shutdown(self, immediate=False):
243247
'''
244248
with self.mutex:
245249
if immediate:
246-
self.shutdown_state = _queue_shutdown_immediate
250+
self.shutdown_state = _QueueState.SHUTDOWN_IMMEDIATE
247251
self.not_empty.notify_all()
248252
# set self.unfinished_tasks to 0
249253
# to break the loop in 'self.join()'
250254
# when quits from `wait()`
251255
self.unfinished_tasks = 0
252256
self.all_tasks_done.notify_all()
253257
else:
254-
self.shutdown_state = _queue_shutdown
258+
self.shutdown_state = _QueueState.SHUTDOWN
255259
self.not_full.notify_all()
256260

257261
# 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.