Skip to content

Commit

Permalink
Merge pull request #455 from wumb0/master
Browse files Browse the repository at this point in the history
fixes #449
  • Loading branch information
comrumino committed Sep 4, 2021
2 parents 2bd5171 + 9a702c4 commit 8518e3b
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions rpyc/core/async_.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time # noqa: F401
from threading import Event
from rpyc.lib import Timeout
from rpyc.lib.compat import TimeoutError as AsyncResultTimeout

Expand All @@ -12,14 +13,14 @@ class AsyncResult(object):

def __init__(self, conn):
self._conn = conn
self._is_ready = False
self._is_ready = Event()
self._is_exc = None
self._obj = None
self._callbacks = []
self._ttl = Timeout(None)

def __repr__(self):
if self._is_ready:
if self._is_ready.is_set():
state = "ready"
elif self._is_exc:
state = "error"
Expand All @@ -34,7 +35,7 @@ def __call__(self, is_exc, obj):
return
self._is_exc = is_exc
self._obj = obj
self._is_ready = True
self._is_ready.set()
for cb in self._callbacks:
cb(self)
del self._callbacks[:]
Expand All @@ -43,9 +44,11 @@ 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 and not self._ttl.expired():
self._conn.serve(self._ttl)
if not self._is_ready:
while not self._is_ready.is_set() and not self._ttl.expired():
if self._conn.serve(self._ttl) == True:
# we received a response, wait for the completion call
self._is_ready.wait()

This comment has been minimized.

Copy link
@TI-AviBerko

TI-AviBerko Sep 19, 2021

This would block forever if the response wasn't handled by another thread

if not self._is_ready.is_set():
raise AsyncResultTimeout("result expired")

def add_callback(self, func):
Expand All @@ -56,7 +59,7 @@ def add_callback(self, func):
:param func: the callback function to add
"""
if self._is_ready:
if self._is_ready.is_set():
func(self)
else:
self._callbacks.append(func)
Expand All @@ -72,12 +75,12 @@ def set_expiry(self, timeout):
@property
def ready(self):
"""Indicates whether the result has arrived"""
if self._is_ready:
if self._is_ready.is_set():
return True
if self._ttl.expired():
return False
self._conn.poll_all()
return self._is_ready
return self._is_ready.is_set()

@property
def error(self):
Expand All @@ -87,7 +90,7 @@ def error(self):
@property
def expired(self):
"""Indicates whether the AsyncResult has expired"""
return not self._is_ready and self._ttl.expired()
return not self._is_ready.is_set() and self._ttl.expired()

@property
def value(self):
Expand Down

0 comments on commit 8518e3b

Please sign in to comment.