0

I am using python2.7 to handle input/output files in GRIB and netCDF format. The code is such that it can read data from both GRIB and netCDF format and write in the format chosen by the user. The del destructor is called to free up the memory. My destructor looks like this:

 def __del__(self):
        """ netCDF : delete the init files and close the dataset
            grib : release grib message (self.msg exists only for GRIB files)
        """
       # GRIB
       if self.msg :
          ga.grib_release(self.msg) 

       # netCDF
       else:
          self.dataset.close()
       try :
           if os.path.isfile(self.fileName): 
                os.remove(self.fileName)
       except :
           pass

Using this destructor slows down may code after few iterations. If I comment out the part for GRIB (when I am using netCDF) or vice-versa, the code runs normally. But using the if loop makes the code very very slow after reading/writing maybe 15 files. Could anyone help me point out the mistake I might be making!

I.K.
  • 13
  • 4

1 Answers1

0

As mentionned in a comment, it's almost impossible to really answer your question without more informations about the context. This being said, there are a couple points worth mentionning...

First and foremost: using __del__ tends to cause more issues than it solves The proper way to handle correct deallocation of resources is to either have a "cleanup" method and make sure to manually call it (might be enough if you're the sole user of this class and this is the only place where it's used), or make your class a context manager.

Second point: having a same class handling two different formats and having the whole class code cluttered with if <this is format one> / else # this is format two checks is a sure design smell. Assumming most of your class code is the same for both format, it looks like you want to extract the format-specific parts to other classes (one per format) with a common API but different implementations, and delegate all format-specific actions to the appropriate one. This is known as the Strategy pattern.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • Thanks for your answer. Somehow, the problem vanishes in a MCVE I created. I will check my code again, maybe the problem is somewhere else! On the other hand, you are right I do have lot of code with if/else checks. I need something better and maybe the strategy pattern could help! – I.K. Apr 10 '18 at 12:48
  • I still reckon you probably shouldn't use `__del__` at all and switch to either the "manual cleanup() call" or (if appliable) the context manager solution. – bruno desthuilliers Apr 10 '18 at 13:21