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

Miss Islington (bot) webhook-mailer at python.org
Wed Jun 5 05:33:30 EDT 2019

commit: 9aa78566fbeeb8cdaa669ad22f92cf63765f4135
branch: master
author: Zackery Spytz <zspytz at gmail.com>
committer: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
date: 2019-06-05T02:33:26-07:00

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


A Misc/NEWS.d/next/Library/2019-06-04-23-44-52.bpo-34767.BpDShN.rst
M Lib/asyncio/locks.py

diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py
index d59eb8f210c7..1324eefb5ff4 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 @@ def locked(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()
@@ -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
             fut = next(iter(self._waiters))
         except StopIteration:
diff --git a/Misc/NEWS.d/next/Library/2019-06-04-23-44-52.bpo-34767.BpDShN.rst b/Misc/NEWS.d/next/Library/2019-06-04-23-44-52.bpo-34767.BpDShN.rst
new file mode 100644
index 000000000000..b46bc44506f4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-06-04-23-44-52.bpo-34767.BpDShN.rst
@@ -0,0 +1 @@
+Do not always create a :class:`collections.deque` in :class:`asyncio.Lock`.

More information about the Python-checkins mailing list