Skip to content

Commit

Permalink
fixing the problem with class AsyncResult when using Threading instea…
Browse files Browse the repository at this point in the history
…d of False
  • Loading branch information
achrafka committed Nov 4, 2021
1 parent bc8f022 commit 1238f48
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions rpyc/core/async_.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import time # noqa: F401
from threading import Event
from rpyc.lib import Timeout
from rpyc.lib.compat import TimeoutError as AsyncResultTimeout

Expand All @@ -13,29 +12,29 @@ 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"
elif self.expired:
state = "expired"
else:
state = "pending"
return f"<AsyncResult object ({state}) at 0x{id(self):08x}>"
return "<AsyncResult object (%s) at 0x%08x>" % (state, id(self))

def __call__(self, is_exc, obj):
if self.expired:
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[:]
Expand All @@ -44,11 +43,9 @@ 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():
if self._conn.serve(self._ttl):
# we received a response, wait for the completion call
self._is_ready.wait()
if not self._is_ready.is_set():
while not self._is_ready and not self._ttl.expired():
self._conn.serve(self._ttl)
if not self._is_ready:
raise AsyncResultTimeout("result expired")

def add_callback(self, func):
Expand All @@ -59,7 +56,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)
Expand All @@ -75,12 +72,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():
return False
self._conn.poll_all()
return self._is_ready.is_set()
return self._is_ready

@property
def error(self):
Expand All @@ -90,7 +87,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):
Expand Down

0 comments on commit 1238f48

Please sign in to comment.