Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7540427

Browse files
committed
Simplify the code using with-statements.
1 parent 2c94cdd commit 7540427

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

Lib/queue.py

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,21 @@ class Queue:
2626
def __init__(self, maxsize=0):
2727
self.maxsize = maxsize
2828
self._init(maxsize)
29+
2930
# mutex must be held whenever the queue is mutating. All methods
3031
# that acquire mutex must release it before returning. mutex
3132
# is shared between the three conditions, so acquiring and
3233
# releasing the conditions also acquires and releases mutex.
3334
self.mutex = _threading.Lock()
35+
3436
# Notify not_empty whenever an item is added to the queue; a
3537
# thread waiting to get is notified then.
3638
self.not_empty = _threading.Condition(self.mutex)
39+
3740
# Notify not_full whenever an item is removed from the queue;
3841
# a thread waiting to put is notified then.
3942
self.not_full = _threading.Condition(self.mutex)
43+
4044
# Notify all_tasks_done whenever the number of unfinished tasks
4145
# drops to zero; thread waiting to join() is notified to resume
4246
self.all_tasks_done = _threading.Condition(self.mutex)
@@ -56,16 +60,13 @@ def task_done(self):
5660
Raises a ValueError if called more times than there were items
5761
placed in the queue.
5862
"""
59-
self.all_tasks_done.acquire()
60-
try:
63+
with self.all_tasks_done:
6164
unfinished = self.unfinished_tasks - 1
6265
if unfinished <= 0:
6366
if unfinished < 0:
6467
raise ValueError('task_done() called too many times')
6568
self.all_tasks_done.notify_all()
6669
self.unfinished_tasks = unfinished
67-
finally:
68-
self.all_tasks_done.release()
6970

7071
def join(self):
7172
"""Blocks until all items in the Queue have been gotten and processed.
@@ -76,19 +77,14 @@ def join(self):
7677
7778
When the count of unfinished tasks drops to zero, join() unblocks.
7879
"""
79-
self.all_tasks_done.acquire()
80-
try:
80+
with self.all_tasks_done:
8181
while self.unfinished_tasks:
8282
self.all_tasks_done.wait()
83-
finally:
84-
self.all_tasks_done.release()
8583

8684
def qsize(self):
8785
"""Return the approximate size of the queue (not reliable!)."""
88-
self.mutex.acquire()
89-
n = self._qsize()
90-
self.mutex.release()
91-
return n
86+
with self.mutex:
87+
return self._qsize()
9288

9389
def empty(self):
9490
"""Return True if the queue is empty, False otherwise (not reliable!).
@@ -102,10 +98,8 @@ def empty(self):
10298
completed, the preferred technique is to use the join() method.
10399
104100
"""
105-
self.mutex.acquire()
106-
n = not self._qsize()
107-
self.mutex.release()
108-
return n
101+
with self.mutex:
102+
return not self._qsize()
109103

110104
def full(self):
111105
"""Return True if the queue is full, False otherwise (not reliable!).
@@ -116,10 +110,8 @@ def full(self):
116110
qsize() can be used.
117111
118112
"""
119-
self.mutex.acquire()
120-
n = 0 < self.maxsize <= self._qsize()
121-
self.mutex.release()
122-
return n
113+
with self.mutex:
114+
return 0 < self.maxsize <= self._qsize()
123115

124116
def put(self, item, block=True, timeout=None):
125117
"""Put an item into the queue.
@@ -132,8 +124,7 @@ def put(self, item, block=True, timeout=None):
132124
is immediately available, else raise the Full exception ('timeout'
133125
is ignored in that case).
134126
"""
135-
self.not_full.acquire()
136-
try:
127+
with self.not_full:
137128
if self.maxsize > 0:
138129
if not block:
139130
if self._qsize() >= self.maxsize:
@@ -153,8 +144,6 @@ def put(self, item, block=True, timeout=None):
153144
self._put(item)
154145
self.unfinished_tasks += 1
155146
self.not_empty.notify()
156-
finally:
157-
self.not_full.release()
158147

159148
def put_nowait(self, item):
160149
"""Put an item into the queue without blocking.
@@ -175,8 +164,7 @@ def get(self, block=True, timeout=None):
175164
available, else raise the Empty exception ('timeout' is ignored
176165
in that case).
177166
"""
178-
self.not_empty.acquire()
179-
try:
167+
with self.not_empty:
180168
if not block:
181169
if not self._qsize():
182170
raise Empty
@@ -195,8 +183,6 @@ def get(self, block=True, timeout=None):
195183
item = self._get()
196184
self.not_full.notify()
197185
return item
198-
finally:
199-
self.not_empty.release()
200186

201187
def get_nowait(self):
202188
"""Remove and return an item from the queue without blocking.

0 commit comments

Comments
 (0)