Commit 9aa78566 authored by Zackery Spytz's avatar Zackery Spytz Committed by Miss Islington (bot)

bpo-34767: Do not always create a collections.deque() in asyncio.Lock() (GH-13834)



https://bugs.python.org/issue34767
parent d4cf099d
...@@ -158,7 +158,7 @@ class Lock(_ContextManagerMixin): ...@@ -158,7 +158,7 @@ class Lock(_ContextManagerMixin):
""" """
def __init__(self, *, loop=None): def __init__(self, *, loop=None):
self._waiters = collections.deque() self._waiters = None
self._locked = False self._locked = False
if loop is not None: if loop is not None:
self._loop = loop self._loop = loop
...@@ -182,10 +182,13 @@ class Lock(_ContextManagerMixin): ...@@ -182,10 +182,13 @@ class Lock(_ContextManagerMixin):
This method blocks until the lock is unlocked, then sets it to This method blocks until the lock is unlocked, then sets it to
locked and returns True. 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 self._locked = True
return True return True
if self._waiters is None:
self._waiters = collections.deque()
fut = self._loop.create_future() fut = self._loop.create_future()
self._waiters.append(fut) self._waiters.append(fut)
...@@ -224,6 +227,8 @@ class Lock(_ContextManagerMixin): ...@@ -224,6 +227,8 @@ class Lock(_ContextManagerMixin):
def _wake_up_first(self): def _wake_up_first(self):
"""Wake up the first waiter if it isn't done.""" """Wake up the first waiter if it isn't done."""
if not self._waiters:
return
try: try:
fut = next(iter(self._waiters)) fut = next(iter(self._waiters))
except StopIteration: except StopIteration:
......
Do not always create a :class:`collections.deque` in :class:`asyncio.Lock`.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment