]> granicus.if.org Git - python/commitdiff
bpo-34767: Do not always create a collections.deque() in asyncio.Lock() (GH-13834)
authorZackery Spytz <zspytz@gmail.com>
Wed, 5 Jun 2019 09:33:27 +0000 (03:33 -0600)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 5 Jun 2019 09:33:26 +0000 (02:33 -0700)
https://bugs.python.org/issue34767

Lib/asyncio/locks.py
Misc/NEWS.d/next/Library/2019-06-04-23-44-52.bpo-34767.BpDShN.rst [new file with mode: 0644]

index d59eb8f210c7e166f5edf057f7039b45a4992bbf..1324eefb5ff46024622eab4375d0bb7c72531e35 100644 (file)
@@ -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:
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 (file)
index 0000000..b46bc44
--- /dev/null
@@ -0,0 +1 @@
+Do not always create a :class:`collections.deque` in :class:`asyncio.Lock`.