[pytango] 13/26: Fix threadpool to allow to raise exception

Sandor Bodo-Merle sbodomerle-guest at moszumanska.debian.org
Thu Sep 28 19:16:14 UTC 2017


This is an automated email from the git hooks/post-receive script.

sbodomerle-guest pushed a commit to annotated tag v8.1.8
in repository pytango.

commit edff0591f369c25d9ff1bda914228ce7f1f63959
Author: Matias Guijarro <matias.guijarro at esrf.fr>
Date:   Mon Sep 28 14:08:52 2015 +0200

    Fix threadpool to allow to raise exception
    
    - Wrap AsynResult to catch exception and return it as result
    - Catch the result to raise again the exception
---
 src/boost/python/tango_gevent.py | 47 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 44 insertions(+), 3 deletions(-)

diff --git a/src/boost/python/tango_gevent.py b/src/boost/python/tango_gevent.py
index 019dbac..60919eb 100644
--- a/src/boost/python/tango_gevent.py
+++ b/src/boost/python/tango_gevent.py
@@ -10,18 +10,59 @@
 # ------------------------------------------------------------------------------
 
 from __future__ import absolute_import
+import sys
+import types
 
 __all__ = ["get_global_threadpool", "get_global_executor", "submit", "spawn"]
 
-
 def get_global_threadpool():
     import gevent
     return gevent.get_hub().threadpool
 
+class ExceptionWrapper:
+    def __init__(self, exception, error_string, tb):
+        self.exception = exception
+        self.error_string = error_string
+        self.tb = tb
 
-def spawn(fn, *args, **kwargs):
-    return get_global_threadpool().spawn(fn, *args, **kwargs)
+class wrap_errors(object):
+    def __init__(self, func):
+        """Make a new function from `func', such that it catches all exceptions
+        and return it as a specific object
+        """
+        self.func = func
+
+    def __call__(self, *args, **kwargs):
+        func = self.func
+        try:
+            return func(*args, **kwargs)
+        except:
+            return ExceptionWrapper(*sys.exc_info())
+
+    def __str__(self):
+        return str(self.func)
 
+    def __repr__(self):
+        return repr(self.func)
+
+    def __getattr__(self, item):
+        return getattr(self.func, item)
+
+def get_with_exception(g, block=True, timeout=None):
+    result = g._get(block, timeout)
+    if isinstance(result, ExceptionWrapper):
+        # raise the exception using the caller context
+        raise result.error_string, None, result.tb
+    else:
+        return result
+
+def spawn(fn, *args, **kwargs):
+    # the gevent threadpool do not raise exception with asyncresults, we have to wrap it
+    fn = wrap_errors(fn)
+    g = get_global_threadpool().spawn(fn, *args, **kwargs)
+    g._get = g.get
+    g.get = types.MethodType(get_with_exception, g)
+    return g
 
 get_global_executor = get_global_threadpool
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/pytango.git



More information about the debian-science-commits mailing list