mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
bpo-34767: Do not always create a collections.deque() in asyncio.Lock() (GH-13834)
https://bugs.python.org/issue34767
(cherry picked from commit 9aa78566fb
)
Co-authored-by: Zackery Spytz <zspytz@gmail.com>
This commit is contained in:
parent
f2054d9565
commit
87a865ec15
2 changed files with 8 additions and 2 deletions
|
@ -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):
|
||||||
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):
|
||||||
|
|
||||||
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:
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Do not always create a :class:`collections.deque` in :class:`asyncio.Lock`.
|
Loading…
Add table
Add a link
Reference in a new issue