Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Lib/asyncio/locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class Lock(_ContextManagerMixin):
"""

def __init__(self, *, loop=None):
self._waiters = collections.deque()
self._waiters = None
self._locked = False
if loop is not None:
self._loop = loop
Expand All @@ -182,10 +182,13 @@ async def acquire(self):
This method blocks until the lock is unlocked, then sets it to
locked and returns True.
"""
if not self._locked and all(w.cancelled() for w in self._waiters):
if (not self._locked and (self._waiters is None or
all(w.cancelled() for w in self._waiters))):
self._locked = True
return True

if self._waiters is None:
self._waiters = collections.deque()
fut = self._loop.create_future()
self._waiters.append(fut)

Expand Down Expand Up @@ -224,6 +227,8 @@ def release(self):

def _wake_up_first(self):
"""Wake up the first waiter if it isn't done."""
if not self._waiters:
return
try:
fut = next(iter(self._waiters))
except StopIteration:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not always create a :class:`collections.deque` in :class:`asyncio.Lock`.