🔀 Merge thodnev/ptb into ptb/promises-with-exceptions

#529
This commit is contained in:
Jannes Höke 2017-02-27 14:45:12 +01:00
commit f8b13440c1
3 changed files with 18 additions and 10 deletions

View file

@ -128,10 +128,16 @@ class ConversationHandler(Handler):
self.logger.debug('waiting for promise...')
old_state, new_state = state
new_state.result(timeout=self.run_async_timeout)
error = False
try:
res = new_state.result(timeout=self.run_async_timeout)
except Exception as exc:
self.logger.exception("promise function raised exception: %s" %
exc)
error = True
if new_state.done.is_set():
self.update_state(new_state.result(), key)
if not error and new_state.done.is_set():
self.update_state(res, key)
state = self.conversations.get(key)
else:

View file

@ -159,11 +159,7 @@ class Dispatcher(object):
len(self.__async_threads))
break
try:
promise.run()
except:
self.logger.exception("run_async function raised exception")
promise.run()
def run_async(self, func, *args, **kwargs):
"""Queue a function (with given args/kwargs) to be run asynchronously.

View file

@ -30,17 +30,23 @@ class Promise(object):
self.kwargs = kwargs
self.done = Event()
self._result = None
self._exception = None
def run(self):
try:
self._result = self.pooled_function(*self.args, **self.kwargs)
except:
raise
except Exception as exc:
self._exception = exc
finally:
self.done.set()
def __call__(self):
self.run()
def result(self, timeout=None):
self.done.wait(timeout=timeout)
if self._exception is not None:
raise self._exception
return self._result