-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reverted _is_ready to bool to remove accidental complexity; changed r…
…ecvlock to rlock since it is used in reentrant functions; added releases as needed to adjust for reentrant acquires
- Loading branch information
Showing
2 changed files
with
34 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,14 +13,14 @@ class AsyncResult(object): | |
|
||
def __init__(self, conn): | ||
self._conn = conn | ||
self._is_ready = Event() | ||
self._is_ready = False | ||
self._is_exc = None | ||
self._obj = None | ||
self._callbacks = [] | ||
self._ttl = Timeout(None) | ||
|
||
def __repr__(self): | ||
if self._is_ready.is_set(): | ||
if self._is_ready: | ||
state = "ready" | ||
elif self._is_exc: | ||
state = "error" | ||
|
@@ -35,7 +35,7 @@ def __call__(self, is_exc, obj): | |
return | ||
self._is_exc = is_exc | ||
self._obj = obj | ||
self._is_ready.set() | ||
self._is_ready = True | ||
for cb in self._callbacks: | ||
cb(self) | ||
del self._callbacks[:] | ||
|
@@ -44,9 +44,14 @@ def wait(self): | |
"""Waits for the result to arrive. If the AsyncResult object has an | ||
expiry set, and the result did not arrive within that timeout, | ||
an :class:`AsyncResultTimeout` exception is raised""" | ||
while not self._is_ready.is_set() and not self._ttl.expired(): | ||
self._conn.serve(self._ttl) | ||
if not self._is_ready.is_set(): | ||
while not (self._is_ready or self.expired): | ||
# Serve the connection since we are not ready. Suppose | ||
# the reply for our seq is served. The callback is this class | ||
# so __call__ sets our obj and _is_ready to true. | ||
self._conn.serve(self._ttl, wait_for_lock=False) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
comrumino
Author
Collaborator
|
||
|
||
# Check if we timed out before result was ready | ||
if not self._is_ready: | ||
raise AsyncResultTimeout("result expired") | ||
|
||
def add_callback(self, func): | ||
|
@@ -57,7 +62,7 @@ def add_callback(self, func): | |
:param func: the callback function to add | ||
""" | ||
if self._is_ready.is_set(): | ||
if self._is_ready: | ||
func(self) | ||
else: | ||
self._callbacks.append(func) | ||
|
@@ -73,12 +78,12 @@ def set_expiry(self, timeout): | |
@property | ||
def ready(self): | ||
"""Indicates whether the result has arrived""" | ||
if self._is_ready.is_set(): | ||
if self._is_ready: | ||
return True | ||
if self._ttl.expired(): | ||
if self.expired: | ||
return False | ||
self._conn.poll_all() | ||
return self._is_ready.is_set() | ||
return self._is_ready | ||
|
||
@property | ||
def error(self): | ||
|
@@ -88,7 +93,7 @@ def error(self): | |
@property | ||
def expired(self): | ||
"""Indicates whether the AsyncResult has expired""" | ||
return not self._is_ready.is_set() and self._ttl.expired() | ||
return not self._is_ready and self._ttl.expired() | ||
|
||
@property | ||
def value(self): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setting
wait_for_lock
toFalse
means that the thread is in a busy loop if another thread is holding the recv lock.