[GRASS-SVN] r64640 - grass/trunk/lib/python/temporal

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Feb 15 15:03:25 PST 2015


Author: huhabla
Date: 2015-02-15 15:03:24 -0800 (Sun, 15 Feb 2015)
New Revision: 64640

Modified:
   grass/trunk/lib/python/temporal/c_libraries_interface.py
Log:
temporal framework: Making the C-library RPC interface more reliable using suggestion from Glynn


Modified: grass/trunk/lib/python/temporal/c_libraries_interface.py
===================================================================
--- grass/trunk/lib/python/temporal/c_libraries_interface.py	2015-02-15 21:07:33 UTC (rev 64639)
+++ grass/trunk/lib/python/temporal/c_libraries_interface.py	2015-02-15 23:03:24 UTC (rev 64640)
@@ -733,12 +733,7 @@
     sys.exit()
 
 ###############################################################################
-# Global server connection
-server_connection = None
-server_lock = None
 
-#def error_handler(data):
-#    server_connection.close()
 
 def c_library_server(lock, conn):
     """The GRASS C-libraries server function designed to be a target for
@@ -747,16 +742,24 @@
        :param lock: A multiprocessing.Lock
        :param conn: A multiprocessing.Pipe
     """
-    #global server_connection
-    #server_connection = conn
- 
-    #CALLBACK = CFUNCTYPE(None, c_void_p)
-    #CALLBACK.restype = None
-    #CALLBACK.argtypes = c_void_p
 
-    #cerror_handler = CALLBACK(error_handler)
+    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")
+        # 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.close()
+        lock.release()
+
+    CALLBACK = CFUNCTYPE(c_void_p, c_void_p)
+    CALLBACK.restype = c_void_p
+    CALLBACK.argtypes = c_void_p
+
+    cerror_handler = CALLBACK(error_handler)
     
-    #libgis.G_add_error_handler(cerror_handler, POINTER(None))
+    libgis.G_add_error_handler(cerror_handler, None)
 
     # Crerate the function array
     functions = [0]*15
@@ -917,10 +920,6 @@
                raise FatalError(message)
            FatalError: Fatal error
            
-           >>> mapset = ciface.get_mapset()
-           >>> location = ciface.get_location()
-           >>> gisdbase = ciface.get_gisdbase()
-           
            >>> ciface.fatal_error()
            Traceback (most recent call last):
                raise FatalError(message)
@@ -936,6 +935,8 @@
                raise FatalError(message)
            FatalError: Fatal error
 
+           >>> ciface.stop()
+
            >>> gscript.del_temp_region()
 
     """
@@ -993,7 +994,6 @@
         """Restart the server if it was terminated
         """
         self.threadLock.acquire()
-        
         if self.server.is_alive() is True:
             self.threadLock.release()
             return
@@ -1329,7 +1329,7 @@
         """
         self.check_server()
         self.client_conn.send([RPCDefs.G_MAPSET, ])
-        return self.safe_receive("get_mapsetn")
+        return self.safe_receive("get_mapset")
 
     def get_location(self):
         """Return the location
@@ -1350,11 +1350,9 @@
         return self.safe_receive("get_gisdbase")
 
     def fatal_error(self, mapset=None):
-        """Return the temporal database name of a specific mapset
-
-           :param mapset: Name of the mapset
-
-           :returns: Name of the database or None if no temporal database present
+        """Generate a fatal error in libgis.
+        
+            This function is only for testing purpose.
         """
         self.check_server()
         self.client_conn.send([RPCDefs.G_FATAL_ERROR])
@@ -1365,8 +1363,11 @@
         """Receive the data and throw an FatalError exception in case the server 
            process was killed and the pipe was closed by the checker thread"""
         try:
-            return self.client_conn.recv()
-        except EOFError:
+            ret = self.client_conn.recv()
+            if isinstance(ret,  FatalError):
+               raise FatalError()
+            return ret
+        except (EOFError,  IOError,  FatalError):
             # The pipe was closed by the checker thread because
             # the server process was killed
             raise FatalError(message)



More information about the grass-commit mailing list