Skip to content

Commit 9aa7856

Browse files
ZackerySpytzmiss-islington
authored andcommitted
bpo-34767: Do not always create a collections.deque() in asyncio.Lock() (GH-13834)
https://bugs.python.org/issue34767
1 parent d4cf099 commit 9aa7856

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

Lib/asyncio/locks.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class Lock(_ContextManagerMixin):
158158
"""
159159

160160
def __init__(self, *, loop=None):
161-
self._waiters = collections.deque()
161+
self._waiters = None
162162
self._locked = False
163163
if loop is not None:
164164
self._loop = loop
@@ -182,10 +182,13 @@ async def acquire(self):
182182
This method blocks until the lock is unlocked, then sets it to
183183
locked and returns True.
184184
"""
185-
if not self._locked and all(w.cancelled() for w in self._waiters):
185+
if (not self._locked and (self._waiters is None or
186+
all(w.cancelled() for w in self._waiters))):
186187
self._locked = True
187188
return True
188189

190+
if self._waiters is None:
191+
self._waiters = collections.deque()
189192
fut = self._loop.create_future()
190193
self._waiters.append(fut)
191194

@@ -224,6 +227,8 @@ def release(self):
224227

225228
def _wake_up_first(self):
226229
"""Wake up the first waiter if it isn't done."""
230+
if not self._waiters:
231+
return
227232
try:
228233
fut = next(iter(self._waiters))
229234
except StopIteration:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Do not always create a :class:`collections.deque` in :class:`asyncio.Lock`.

0 commit comments

Comments
 (0)