@@ -26,17 +26,21 @@ class Queue:
26
26
def __init__ (self , maxsize = 0 ):
27
27
self .maxsize = maxsize
28
28
self ._init (maxsize )
29
+
29
30
# mutex must be held whenever the queue is mutating. All methods
30
31
# that acquire mutex must release it before returning. mutex
31
32
# is shared between the three conditions, so acquiring and
32
33
# releasing the conditions also acquires and releases mutex.
33
34
self .mutex = _threading .Lock ()
35
+
34
36
# Notify not_empty whenever an item is added to the queue; a
35
37
# thread waiting to get is notified then.
36
38
self .not_empty = _threading .Condition (self .mutex )
39
+
37
40
# Notify not_full whenever an item is removed from the queue;
38
41
# a thread waiting to put is notified then.
39
42
self .not_full = _threading .Condition (self .mutex )
43
+
40
44
# Notify all_tasks_done whenever the number of unfinished tasks
41
45
# drops to zero; thread waiting to join() is notified to resume
42
46
self .all_tasks_done = _threading .Condition (self .mutex )
@@ -56,16 +60,13 @@ def task_done(self):
56
60
Raises a ValueError if called more times than there were items
57
61
placed in the queue.
58
62
"""
59
- self .all_tasks_done .acquire ()
60
- try :
63
+ with self .all_tasks_done :
61
64
unfinished = self .unfinished_tasks - 1
62
65
if unfinished <= 0 :
63
66
if unfinished < 0 :
64
67
raise ValueError ('task_done() called too many times' )
65
68
self .all_tasks_done .notify_all ()
66
69
self .unfinished_tasks = unfinished
67
- finally :
68
- self .all_tasks_done .release ()
69
70
70
71
def join (self ):
71
72
"""Blocks until all items in the Queue have been gotten and processed.
@@ -76,19 +77,14 @@ def join(self):
76
77
77
78
When the count of unfinished tasks drops to zero, join() unblocks.
78
79
"""
79
- self .all_tasks_done .acquire ()
80
- try :
80
+ with self .all_tasks_done :
81
81
while self .unfinished_tasks :
82
82
self .all_tasks_done .wait ()
83
- finally :
84
- self .all_tasks_done .release ()
85
83
86
84
def qsize (self ):
87
85
"""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 ()
92
88
93
89
def empty (self ):
94
90
"""Return True if the queue is empty, False otherwise (not reliable!).
@@ -102,10 +98,8 @@ def empty(self):
102
98
completed, the preferred technique is to use the join() method.
103
99
104
100
"""
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 ()
109
103
110
104
def full (self ):
111
105
"""Return True if the queue is full, False otherwise (not reliable!).
@@ -116,10 +110,8 @@ def full(self):
116
110
qsize() can be used.
117
111
118
112
"""
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 ()
123
115
124
116
def put (self , item , block = True , timeout = None ):
125
117
"""Put an item into the queue.
@@ -132,8 +124,7 @@ def put(self, item, block=True, timeout=None):
132
124
is immediately available, else raise the Full exception ('timeout'
133
125
is ignored in that case).
134
126
"""
135
- self .not_full .acquire ()
136
- try :
127
+ with self .not_full :
137
128
if self .maxsize > 0 :
138
129
if not block :
139
130
if self ._qsize () >= self .maxsize :
@@ -153,8 +144,6 @@ def put(self, item, block=True, timeout=None):
153
144
self ._put (item )
154
145
self .unfinished_tasks += 1
155
146
self .not_empty .notify ()
156
- finally :
157
- self .not_full .release ()
158
147
159
148
def put_nowait (self , item ):
160
149
"""Put an item into the queue without blocking.
@@ -175,8 +164,7 @@ def get(self, block=True, timeout=None):
175
164
available, else raise the Empty exception ('timeout' is ignored
176
165
in that case).
177
166
"""
178
- self .not_empty .acquire ()
179
- try :
167
+ with self .not_empty :
180
168
if not block :
181
169
if not self ._qsize ():
182
170
raise Empty
@@ -195,8 +183,6 @@ def get(self, block=True, timeout=None):
195
183
item = self ._get ()
196
184
self .not_full .notify ()
197
185
return item
198
- finally :
199
- self .not_empty .release ()
200
186
201
187
def get_nowait (self ):
202
188
"""Remove and return an item from the queue without blocking.
0 commit comments