Will's blog

purpose: Will Kahn-Greene's blog of Miro, PyBlosxom, Python, GNU/Linux, random content, PyBlosxom, Miro, and other projects mixed in there ad hoc, half-baked, and with a twist of lemon

[ home | blog home | recent activity ]

Sun, 15 Feb 2004

Tip for storing data between Pyblosxom calls

Some plugins need to store data between Pyblosxom requests. Take for example my viewcounts plugin which keeps track of how many times a given entry has been viewed. This data is stored as a pickled dict of entryid -> view count in a viewcounts.dat file in my datadir.

My viewcounts plugin does two things. First, it displays the current number of times a given entry has been viewed. Second, it updates this number. This creates a class read/write serialization problem where two or more processes could be trying to read and modify the data at the same time. This is solved by serializing the processes so that only one process is reading and modifying the data at a time.

There are a couple of ways you can store this data. First, you could store it in a database--which handles serialization and all that sort of stuff for you in transactions.

The other way is to use the tools module's lock/unlock functions (from the Python Cookbook portalocker code). For example:

       from Pyblosxom import tools
       import pickle

       def cb_start(args):
           request = args["request"]
           data = request.getData()
           config = request.getConfiguration()
           datadir = config["datadir"]

           f = file(datadir + "/mydatafile.dat", "r+")
           tools.lock(f, tools.LOCK_EX)

           data["myplugin_datafile"] = f
           data["myplugin_data"] = pickle.load(f)


       def cb_story(args):
           # We can use the data file anywhere--I'll just use it
           # here to show the example code.
           request = args["request"]
           data = request.getData()

           # get the data we saved in cb_start....
           d = data["myplugin_data"]

           # do our data modification here...
           ...


       def cb_end(args):
           request = args["request"]
           data = request.getData()

           f = data[""myplugin_datafile"]
           d = data["myplugin_data"]

           # set the pointer back to the beginning of the file
           f.seek(0, 0)

           # dump the modified data back to the file
           pickle.dump(d, f, 1)

           # close the file
           f.close()

           tools.unlock(f)
   

That's pretty much it.

changelog

Comments:

Post a new comment:

Three things:

  1. New comments get placed in a "draft" status and will NOT show up on the site until I explicitly approve it. Sometimes that happens within 24 hours.
  2. I reserve the right to reject/remove inappropriate comments.
  3. Sometimes I'll reply to a comment directly in email--so make sure your email address is correct.

If you can't for some reason post a comment, send me an email: willg at bluesock dot org.

Your name:


Your e-mail address (this doesn't get displayed to anyone--sometimes I'll reply directly to you):


URL of your website (optional):


Comment:


Yes, I am a human!


pyblosxom::1.5-dev git-master

Copyright 1996 to 2012, Will Guaraldi Kahn-Greene, under the Creative Commons BY-SA 3.0 license

Creative Commons License
Will's Blog by William Kahn-Greene is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.