Browse Source

pygrass rpc: Better error handling in rpc server

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@66540 15284696-431f-4ddb-bdfa-cd5b030d7da7
Soeren Gebbert 9 years ago
parent
commit
809d2ef515
2 changed files with 22 additions and 10 deletions
  1. 14 2
      lib/python/pygrass/rpc/__init__.py
  2. 8 8
      lib/python/pygrass/rpc/base.py

+ 14 - 2
lib/python/pygrass/rpc/__init__.py

@@ -198,11 +198,11 @@ def data_provider_server(lock, conn):
 
     def error_handler(data):
         """This function will be called in case of a fatal error in libgis"""
-        #sys.stderr.write("Error handler was called\n")
+        # sys.stderr.write("Error handler was called\n")
         # We send an exeption that will be handled in
         # the parent process, then close the pipe
         # and release any possible lock
-        conn.send(FatalError())
+        conn.send(FatalError("G_fatal_error() was called in the server process"))
         conn.close()
         lock.release()
 
@@ -278,8 +278,20 @@ class DataProvider(RPCServerBase):
             >>> len(ret)
             12
 
+            >>> extent = {"north":100, "south":10, "east":30, "west":10,
+            ...           "rows":2, "cols":2}
+            >>> ret = provider.get_raster_image_as_np(name=test_raster_name,
+            ...                                       extent=extent)
+
             >>> provider.stop()
 
+            >>> extent = {"rows":3, "cols":1}
+            >>> ret = provider.get_raster_image_as_np(name=test_raster_name,
+            ...                                       extent=extent)
+            >>> len(ret)
+            12
+
+
             ..
         """
         self.check_server()

+ 8 - 8
lib/python/pygrass/rpc/base.py

@@ -115,8 +115,8 @@ class RPCServerBase(object):
         """Check every 200 micro seconds if the server process is alive"""
         while True:
             time.sleep(0.2)
-            #sys.stderr.write("Check server process\n")
-            self._check_restart_server()
+            # sys.stderr.write("Check server process\n")
+            self._check_restart_server(caller="Server check thread")
             self.threadLock.acquire()
             if self.stopThread == True:
                 #sys.stderr.write("Stop thread\n")
@@ -137,7 +137,7 @@ class RPCServerBase(object):
     def check_server(self):
         self._check_restart_server()
 
-    def _check_restart_server(self):
+    def _check_restart_server(self, caller="main thread"):
         """Restart the server if it was terminated
         """
         self.threadLock.acquire()
@@ -148,22 +148,22 @@ class RPCServerBase(object):
         self.server_conn.close()
         self.start_server()
 
-        logging.warning("Needed to restart the libgis server")
+        logging.warning("Needed to restart the libgis server, caller: %s"%(caller))
 
         self.threadLock.release()
 
     def safe_receive(self, message):
-        """Receive the data and throw an FatalError exception in case the server
+        """Receive the data and throw a FatalError exception in case the server
            process was killed and the pipe was closed by the checker thread"""
         try:
             ret = self.client_conn.recv()
             if isinstance(ret,  FatalError):
-               raise FatalError()
+               raise ret
             return ret
-        except (EOFError,  IOError,  FatalError):
+        except (EOFError,  IOError,  FatalError), e:
             # The pipe was closed by the checker thread because
             # the server process was killed
-            raise FatalError(message)
+            raise FatalError(message + "\n  " + str(e))
 
     def stop(self):
         """Stop the check thread, the libgis server and close the pipe