@@ -84,12 +84,18 @@ async def __aexit__(self, exc_type: Any, exc: Any, tb: Any) -> None:
84
84
self .release ()
85
85
86
86
87
+ def _safe_set_result (fut : asyncio .Future ) -> None :
88
+ # Ensure the future hasn't been cancelled before calling set_result.
89
+ if not fut .done ():
90
+ fut .set_result (False )
91
+
92
+
87
93
class _ACondition :
88
94
__slots__ = ("_condition" , "_waiters" )
89
95
90
96
def __init__ (self , condition : threading .Condition ) -> None :
91
97
self ._condition = condition
92
- self ._waiters = collections .deque ()
98
+ self ._waiters : collections . deque = collections .deque ()
93
99
94
100
async def acquire (self , blocking : bool = True , timeout : float = - 1 ) -> bool :
95
101
if timeout > 0 :
@@ -157,20 +163,7 @@ async def wait(self, timeout: Optional[float] = None) -> bool:
157
163
self .notify (1 )
158
164
raise
159
165
160
- async def wait_for (self , predicate ):
161
- """Wait until a predicate becomes true.
162
-
163
- The predicate should be a callable which result will be
164
- interpreted as a boolean value. The final predicate value is
165
- the return value.
166
- """
167
- result = predicate ()
168
- while not result :
169
- await self .wait ()
170
- result = predicate ()
171
- return result
172
-
173
- def notify (self , n = 1 ):
166
+ def notify (self , n : int = 1 ) -> None :
174
167
"""By default, wake up one coroutine waiting on this condition, if any.
175
168
If the calling coroutine has not acquired the lock when this method
176
169
is called, a RuntimeError is raised.
@@ -191,12 +184,8 @@ def notify(self, n=1):
191
184
if fut .done ():
192
185
continue
193
186
194
- def safe_set_result (fut ):
195
- if not fut .done ():
196
- fut .set_result (False )
197
-
198
187
try :
199
- loop .call_soon_threadsafe (safe_set_result , fut )
188
+ loop .call_soon_threadsafe (_safe_set_result , fut )
200
189
except RuntimeError :
201
190
# Loop was closed, ignore.
202
191
to_remove .append ((loop , fut ))
@@ -207,7 +196,7 @@ def safe_set_result(fut):
207
196
for waiter in to_remove :
208
197
self ._waiters .remove (waiter )
209
198
210
- def notify_all (self ):
199
+ def notify_all (self ) -> None :
211
200
"""Wake up all threads waiting on this condition. This method acts
212
201
like notify(), but wakes up all waiting threads instead of one. If the
213
202
calling thread has not acquired the lock when this method is called,
0 commit comments