diff options
author | Zackery Spytz <zspytz@gmail.com> | 2019-06-05 09:33:27 (GMT) |
---|---|---|
committer | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2019-06-05 09:33:26 (GMT) |
commit | 9aa78566fbeeb8cdaa669ad22f92cf63765f4135 (patch) | |
tree | 55e1d3225f335ca1dea38cd9780f965f063cb2fd /Lib/asyncio | |
parent | d4cf099dff4720a25208b5fa247dc16d86b11ac3 (diff) | |
download | cpython-9aa78566fbeeb8cdaa669ad22f92cf63765f4135.zip cpython-9aa78566fbeeb8cdaa669ad22f92cf63765f4135.tar.gz cpython-9aa78566fbeeb8cdaa669ad22f92cf63765f4135.tar.bz2 |
bpo-34767: Do not always create a collections.deque() in asyncio.Lock() (GH-13834)
https://bugs.python.org/issue34767
Diffstat (limited to 'Lib/asyncio')
-rw-r--r-- | Lib/asyncio/locks.py | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py index d59eb8f..1324eef 100644 --- a/Lib/asyncio/locks.py +++ b/Lib/asyncio/locks.py @@ -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 @@ -182,10 +182,13 @@ class Lock(_ContextManagerMixin): 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) @@ -224,6 +227,8 @@ class Lock(_ContextManagerMixin): 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: |