Commit 59a537fe authored by Jason Madden's avatar Jason Madden

Avoid a circular ref that broke the leaktests. Bonus: it's actually faster on Py3

parent 50a3130b
...@@ -29,6 +29,7 @@ class _Condition(object): ...@@ -29,6 +29,7 @@ class _Condition(object):
def __init__(self, lock): def __init__(self, lock):
self.__lock = lock self.__lock = lock
self.__waiters = []
# If the lock defines _release_save() and/or _acquire_restore(), # If the lock defines _release_save() and/or _acquire_restore(),
# these override the default implementations (which just call # these override the default implementations (which just call
...@@ -45,17 +46,12 @@ class _Condition(object): ...@@ -45,17 +46,12 @@ class _Condition(object):
self._is_owned = lock._is_owned self._is_owned = lock._is_owned
except AttributeError: except AttributeError:
pass pass
self.__waiters = []
# Capture the bound method to save some time returning it
self.__notify_one = self._notify_one
self.__wait = self._wait
def __enter__(self): def __enter__(self):
self.__lock.__enter__() return self.__lock.__enter__()
return self.__wait, self.__notify_one
def __exit__(self, *args): def __exit__(self, t, v, tb):
return self.__lock.__exit__(*args) return self.__lock.__exit__(t, v, tb)
def __repr__(self): def __repr__(self):
return "<Condition(%s, %d)>" % (self.__lock, len(self.__waiters)) return "<Condition(%s, %d)>" % (self.__lock, len(self.__waiters))
...@@ -74,10 +70,8 @@ class _Condition(object): ...@@ -74,10 +70,8 @@ class _Condition(object):
return False return False
return True return True
def _wait(self): def wait(self):
# The condition MUST be owned; the only way to get this # The condition MUST be owned, but we don't check that.
# method is through __enter__, so it is guaranteed and we don't
# need to check it.
waiter = Lock() waiter = Lock()
waiter.acquire() waiter.acquire()
self.__waiters.append(waiter) self.__waiters.append(waiter)
...@@ -87,10 +81,8 @@ class _Condition(object): ...@@ -87,10 +81,8 @@ class _Condition(object):
finally: finally:
self._acquire_restore(saved_state) self._acquire_restore(saved_state)
def _notify_one(self): def notify_one(self):
# The condition MUST be owned; the only way to get this # The condition MUST be owned, but we don't check that.
# method is through __enter__, so it is guaranteed and we
# don't need to check it.
try: try:
waiter = self.__waiters.pop() waiter = self.__waiters.pop()
except IndexError: except IndexError:
...@@ -157,16 +149,16 @@ class Queue(object): ...@@ -157,16 +149,16 @@ class Queue(object):
def put(self, item): def put(self, item):
"""Put an item into the queue. """Put an item into the queue.
""" """
with self._not_empty as (_, notify_one): with self._not_empty:
self._queue.append(item) self._queue.append(item)
self.unfinished_tasks += 1 self.unfinished_tasks += 1
notify_one() self._not_empty.notify_one()
def get(self): def get(self):
"""Remove and return an item from the queue. """Remove and return an item from the queue.
""" """
with self._not_empty as (wait, _): with self._not_empty:
while not self._queue: while not self._queue:
wait() self._not_empty.wait()
item = self._queue.popleft() item = self._queue.popleft()
return item return item
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment