title: Saving and recovering values of variables between executions
body: <p>I have a Python module that is operating as a server for a wireless handheld computer.  Every time the handheld sends a message to the server, the module determines what kind of message it is, and then assembles an appropriate response.  Because the responses are often state-dependent, I am using global variables where needed to retain/share information between the individual functions that handle each type of message.</p>
  
  <p>The problem I'm having is when the application is closed (for whatever reason), the global variable values are (of course) lost, so on re-launching the application it's out of synch with the handheld.  I need a reliable way to store those values for recovery.</p>
  
  <p>The direction I've gone so far (but have not gotten it to work yet) is to write the variable names and their values to a CSV file on the disk, every time they're updated -- and then (when the app is launched), look for that file and use it to assign the variables to their previous states.  I have no trouble writing the file or reading it, but for some reason the values just aren't getting assigned.</p>
  
  <p>I can post the code for comments/help, but before that I wanted to find out whether I'm just going an entirely wrong direction in the first place.  Is there a better (or at least preferable) way to save and recover these values?</p>
  
  <p>thanks,
  JDM</p>
  
  <p>====</p>
  
  <p>Following up.  It may be a touch klunky, but here's what I have and it's working.  The only globals I care about are the ones that start with "CUR_".  I had to use <code>tempDict1</code> because the interpreter doesn't seem to like iterating directly over <code>globals()</code>.</p>
  
  <pre>
      import pickle
      CUR_GLO1 = 'valglo1'
      CUR_GLO2 = 'valglo2'
      CUR_GLO3 = 'valglo3'
  
      def saveGlobs():
          tempDict1 = globals().copy()
          tempDict2 = {}
          for key in tempDict1:
              if (key[:4]=='CUR_'):tempDict2[key] = tempDict1[key]
          pickle.dump(tempDict2,open('tempDict.p','wb'))
  
      def retrieveGlobs():
          tempDict = pickle.load(open('tempDict.p','rb'))
          globals().update(tempDict)
  </pre>