[GRASS-dev] [GRASS GIS] #2134: Create a general exit-safe interface to C libraries

GRASS GIS trac at osgeo.org
Thu Nov 28 01:57:33 PST 2013


#2134: Create a general exit-safe interface to C libraries
--------------------------------------------------+-------------------------
 Reporter:  wenzeslaus                            |       Owner:  grass-dev@…              
     Type:  enhancement                           |      Status:  new                      
 Priority:  normal                                |   Milestone:  7.0.0                    
Component:  Python ctypes                         |     Version:  svn-trunk                
 Keywords:  G_fatal_error, exit, multiprocessing  |    Platform:  All                      
      Cpu:  Unspecified                           |  
--------------------------------------------------+-------------------------

Comment(by zarch):

 Hi Soeren,

 sorry for the late reply.

 Replying to [comment:15 huhabla]:
 > I have attached a new RPC server that is able to instantiate a
 persistent
 > PyGRASS object in a remote process that can be accessed using an RPC
 > interface

 Thank you for that, I think it is a good solution to the problem.


 > To get it work i needed to patch the PyGRASS raster info object to
 > avoid C-pointer that can not be pickled,
 > IMHO PyGRASS should avoid pointer and should use ctypes.byref() to
 > pass pointers to GRASS C-functions. I think (i am not sure about this)
 > that avoiding ctypes.pointer() may reduce memory leaks as well
 > if objects like Cell_head() of Range() are managed by the Python
 > garbage collector?

 Yes, exactly, remove the pointer to the C structs will make independent
 all the objects to each other, so should solve the problem of the circular
 reference and memory consumption that I cited in a previous email.

 So I can remove all the pointers, or leave the pointer but instantiate an
 independent C struct internally on the object, like looking at your diff
 [[pygrass_raster.diff]], I could do in the init:

 {{{

 def __init__(self, ...):
     ...
     self.cs_region = libgis.Cell_head()  # C struct region
     self.c_region = ctypes.pointer(self.cs_region)  # C pointer
     ...

 }}}

 And all the time that I use a C struct pointer to instantiate a class I
 need to copy the struct internally with memcpy. In this way, when the
 python object is dereferenced, the garbage collector should be able to
 manage and free the memory.
 Of course from a performance point of view we loose something because
 every time that we instantiate a geometry feature we are copying the
 "MapInfo" struct.

 Do you have a better idea?

-- 
Ticket URL: <http://trac.osgeo.org/grass/ticket/2134#comment:16>
GRASS GIS <http://grass.osgeo.org>



More information about the grass-dev mailing list