1

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.

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.

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.

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?

thanks, JDM

====

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 tempDict1 because the interpreter doesn't seem to like iterating directly over globals().

    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)
JDM
  • 1,709
  • 3
  • 25
  • 48
  • 1
    The [pickle module](http://docs.python.org/2/library/pickle.html) is probably easier to use if you want to conveniently store and retrieve Python variables. There's an easy example [here](http://wiki.python.org/moin/UsingPickle) – m01 Dec 15 '12 at 00:00
  • Will that work with separate globals (as I have it now)? Or would I need to switch the storage to a dictionary/list/whatever? Part of what I'm trying to accomplish is **not** having to update the save/recover portion of the code every time I need to add a new piece of information to the state info. – JDM Dec 15 '12 at 05:34
  • Thanks, @m01. It looks like `pickle` will do the job nicely. – JDM Dec 16 '12 at 01:01

1 Answers1

2

writing it up as an answer..

What I think you want to do is a form of application checkpointing.

You can use the Pickle module for conveniently saving and loading Python variables. Here is a simple example of how to use it. This discussion on Stackoverflow and this note seem to agree, although part of me thinks that there must be a better way.

Incidentally, you don't need to put everything into a dictionary. As long as you dump and load variables in the right order, and make sure that you don't change that, insert data in the middle etc, you can just dump and load several variables. Using a dictionary like you did does remove the ordering dependency though.

% python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> foo=123
>>> bar="hello"
>>> d={'abc': 123, 'def': 456}
>>> f=open('p.pickle', 'wb')
>>> pickle.dump(foo, f)
>>> pickle.dump(bar, f)
>>> pickle.dump(d, f)
>>> ^D
% python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> f=open('p.pickle','rb')
>>> foo=pickle.load(f)
>>> foo
123
>>> bar=pickle.load(f)
>>> bar
'hello'
>>> d=pickle.load(f)
>>> d
{'abc': 123, 'def': 456}
>>> 
Community
  • 1
  • 1
m01
  • 9,033
  • 6
  • 32
  • 58
  • 1
    Yes, checkpointing is exactly the goal. Thanks again for pointing me in a better direction. – JDM Dec 17 '12 at 14:41
  • @m01, did you ever find a better way? – timbram May 18 '15 at 17:45
  • You should look into journals/journaling, see e.g. the Wikipedia page on [journal (computing)](http://en.wikipedia.org/w/index.php?title=Journal_(computing)) for a crude introduction. I'm not sure if there are any good Python libraries for it, I haven't researched it in the context of Python. Some filesystems (ext, hfs) and databases are prominent users of journaling. The Scala akka libraries also [facilitate using journaling](http://doc.akka.io/docs/akka/snapshot/scala/persistence.html#Architecture) for playing back messages after events like restarts. – m01 May 25 '15 at 10:29