Skip to content

Commit 5144326

Browse files
committed
PYTHON-4782 Cleanup
1 parent d7dc659 commit 5144326

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

pymongo/lock.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,18 @@ async def __aexit__(self, exc_type: Any, exc: Any, tb: Any) -> None:
8484
self.release()
8585

8686

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+
8793
class _ACondition:
8894
__slots__ = ("_condition", "_waiters")
8995

9096
def __init__(self, condition: threading.Condition) -> None:
9197
self._condition = condition
92-
self._waiters = collections.deque()
98+
self._waiters: collections.deque = collections.deque()
9399

94100
async def acquire(self, blocking: bool = True, timeout: float = -1) -> bool:
95101
if timeout > 0:
@@ -157,20 +163,7 @@ async def wait(self, timeout: Optional[float] = None) -> bool:
157163
self.notify(1)
158164
raise
159165

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:
174167
"""By default, wake up one coroutine waiting on this condition, if any.
175168
If the calling coroutine has not acquired the lock when this method
176169
is called, a RuntimeError is raised.
@@ -191,12 +184,8 @@ def notify(self, n=1):
191184
if fut.done():
192185
continue
193186

194-
def safe_set_result(fut):
195-
if not fut.done():
196-
fut.set_result(False)
197-
198187
try:
199-
loop.call_soon_threadsafe(safe_set_result, fut)
188+
loop.call_soon_threadsafe(_safe_set_result, fut)
200189
except RuntimeError:
201190
# Loop was closed, ignore.
202191
to_remove.append((loop, fut))
@@ -207,7 +196,7 @@ def safe_set_result(fut):
207196
for waiter in to_remove:
208197
self._waiters.remove(waiter)
209198

210-
def notify_all(self):
199+
def notify_all(self) -> None:
211200
"""Wake up all threads waiting on this condition. This method acts
212201
like notify(), but wakes up all waiting threads instead of one. If the
213202
calling thread has not acquired the lock when this method is called,

0 commit comments

Comments
 (0)