Commit 6da736e0 authored by Denis Bilenko's avatar Denis Bilenko

ThreadPool: add apply_e() method

parent 644df38d
......@@ -186,6 +186,16 @@ class ThreadPool(object):
if need_decrease:
self._decrease_size()
def apply_e(self, expected_errors, function, args=None, kwargs=None):
if args is None:
args = ()
if kwargs is None:
kwargs = {}
success, result = self.spawn(wrap_errors, expected_errors, function, args, kwargs).get()
if success:
return result
raise result
def apply(self, func, args=None, kwds=None):
"""Equivalent of the apply() builtin function. It blocks till the result is ready."""
if args is None:
......@@ -281,3 +291,10 @@ class ThreadResult(object):
# link protocol:
def successful(self):
return True
def wrap_errors(errors, function, args, kwargs):
try:
return True, function(*args, **kwargs)
except errors:
return False, sys.exc_info()[1]
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