瀏覽代碼

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 年之前
父節點
當前提交
809d2ef515
共有 2 個文件被更改,包括 22 次插入10 次删除
  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):
     def error_handler(data):
         """This function will be called in case of a fatal error in libgis"""
         """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
         # We send an exeption that will be handled in
         # the parent process, then close the pipe
         # the parent process, then close the pipe
         # and release any possible lock
         # and release any possible lock
-        conn.send(FatalError())
+        conn.send(FatalError("G_fatal_error() was called in the server process"))
         conn.close()
         conn.close()
         lock.release()
         lock.release()
 
 
@@ -278,8 +278,20 @@ class DataProvider(RPCServerBase):
             >>> len(ret)
             >>> len(ret)
             12
             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()
             >>> 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()
         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"""
         """Check every 200 micro seconds if the server process is alive"""
         while True:
         while True:
             time.sleep(0.2)
             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()
             self.threadLock.acquire()
             if self.stopThread == True:
             if self.stopThread == True:
                 #sys.stderr.write("Stop thread\n")
                 #sys.stderr.write("Stop thread\n")
@@ -137,7 +137,7 @@ class RPCServerBase(object):
     def check_server(self):
     def check_server(self):
         self._check_restart_server()
         self._check_restart_server()
 
 
-    def _check_restart_server(self):
+    def _check_restart_server(self, caller="main thread"):
         """Restart the server if it was terminated
         """Restart the server if it was terminated
         """
         """
         self.threadLock.acquire()
         self.threadLock.acquire()
@@ -148,22 +148,22 @@ class RPCServerBase(object):
         self.server_conn.close()
         self.server_conn.close()
         self.start_server()
         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()
         self.threadLock.release()
 
 
     def safe_receive(self, message):
     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"""
            process was killed and the pipe was closed by the checker thread"""
         try:
         try:
             ret = self.client_conn.recv()
             ret = self.client_conn.recv()
             if isinstance(ret,  FatalError):
             if isinstance(ret,  FatalError):
-               raise FatalError()
+               raise ret
             return ret
             return ret
-        except (EOFError,  IOError,  FatalError):
+        except (EOFError,  IOError,  FatalError), e:
             # The pipe was closed by the checker thread because
             # The pipe was closed by the checker thread because
             # the server process was killed
             # the server process was killed
-            raise FatalError(message)
+            raise FatalError(message + "\n  " + str(e))
 
 
     def stop(self):
     def stop(self):
         """Stop the check thread, the libgis server and close the pipe
         """Stop the check thread, the libgis server and close the pipe