Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions Lib/asyncio/locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class Event(mixins._LoopBoundMixin):
"""

def __init__(self):
self._waiters = collections.deque()
self._waiters = None
self._value = False

def __repr__(self):
Expand All @@ -187,9 +187,10 @@ def set(self):
if not self._value:
self._value = True

for fut in self._waiters:
if not fut.done():
fut.set_result(True)
if self._waiters:
for fut in self._waiters:
if not fut.done():
fut.set_result(True)

def clear(self):
"""Reset the internal flag to false. Subsequently, tasks calling
Expand All @@ -207,6 +208,8 @@ async def wait(self):
if self._value:
return True

if self._waiters is None:
self._waiters = collections.deque()
fut = self._get_loop().create_future()
self._waiters.append(fut)
try:
Expand Down Expand Up @@ -236,7 +239,7 @@ def __init__(self, lock=None):
self.acquire = lock.acquire
self.release = lock.release

self._waiters = collections.deque()
self._waiters = None

def __repr__(self):
res = super().__repr__()
Expand All @@ -263,6 +266,8 @@ async def wait(self):
if not self.locked():
raise RuntimeError('cannot wait on un-acquired lock')

if self._waiters is None:
self._waiters = collections.deque()
fut = self._get_loop().create_future()
self.release()
try:
Expand Down Expand Up @@ -331,6 +336,8 @@ def notify(self, n=1):
self._notify(n)

def _notify(self, n):
if not self._waiters:
return
idx = 0
for fut in self._waiters:
if idx >= n:
Expand All @@ -346,7 +353,7 @@ def notify_all(self):
calling task has not acquired the lock when this method is called,
a RuntimeError is raised.
"""
self.notify(len(self._waiters))
self.notify(len(self._waiters or ()))


class Semaphore(_ContextManagerMixin, mixins._LoopBoundMixin):
Expand Down
Loading