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

Skip to content

Commit 71ed220

Browse files
committed
Simplified the new get/get_nowait/put/put_nowait implementations a bit.
1 parent 5af0e41 commit 71ed220

1 file changed

Lines changed: 12 additions & 27 deletions

File tree

Lib/Queue.py

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,12 @@ def put(self, item, block=True, timeout=None):
6868
is immediately available, else raise the Full exception ('timeout'
6969
is ignored in that case).
7070
"""
71-
if not block:
72-
return self.put_nowait(item)
7371
self.not_full.acquire()
7472
try:
75-
if timeout is None:
73+
if not block:
74+
if self._full():
75+
raise Full
76+
elif timeout is None:
7677
while self._full():
7778
self.not_full.wait()
7879
else:
@@ -81,7 +82,7 @@ def put(self, item, block=True, timeout=None):
8182
endtime = _time() + timeout
8283
while self._full():
8384
remaining = endtime - _time()
84-
if remaining < 0.0:
85+
if remaining <= 0.0:
8586
raise Full
8687
self.not_full.wait(remaining)
8788
self._put(item)
@@ -95,15 +96,7 @@ def put_nowait(self, item):
9596
Only enqueue the item if a free slot is immediately available.
9697
Otherwise raise the Full exception.
9798
"""
98-
self.not_full.acquire()
99-
try:
100-
if self._full():
101-
raise Full
102-
else:
103-
self._put(item)
104-
self.not_empty.notify()
105-
finally:
106-
self.not_full.release()
99+
return self.put(item, False)
107100

108101
def get(self, block=True, timeout=None):
109102
"""Remove and return an item from the queue.
@@ -116,11 +109,12 @@ def get(self, block=True, timeout=None):
116109
available, else raise the Empty exception ('timeout' is ignored
117110
in that case).
118111
"""
119-
if not block:
120-
return self.get_nowait()
121112
self.not_empty.acquire()
122113
try:
123-
if timeout is None:
114+
if not block:
115+
if self._empty():
116+
raise Empty
117+
elif timeout is None:
124118
while self._empty():
125119
self.not_empty.wait()
126120
else:
@@ -129,7 +123,7 @@ def get(self, block=True, timeout=None):
129123
endtime = _time() + timeout
130124
while self._empty():
131125
remaining = endtime - _time()
132-
if remaining < 0.0:
126+
if remaining <= 0.0:
133127
raise Empty
134128
self.not_empty.wait(remaining)
135129
item = self._get()
@@ -144,16 +138,7 @@ def get_nowait(self):
144138
Only get an item if one is immediately available. Otherwise
145139
raise the Empty exception.
146140
"""
147-
self.not_empty.acquire()
148-
try:
149-
if self._empty():
150-
raise Empty
151-
else:
152-
item = self._get()
153-
self.not_full.notify()
154-
return item
155-
finally:
156-
self.not_empty.release()
141+
return self.get(False)
157142

158143
# Override these methods to implement other queue organizations
159144
# (e.g. stack or priority queue).

0 commit comments

Comments
 (0)