[mod_python] Problem with caching

Graham Dumpleton graham.dumpleton at gmail.com
Fri Apr 3 19:28:55 EDT 2009


2009/4/4 Aaron Scott Hildebrandt <aaron.hildebrandt at gmail.com>:
> This is my first time working with mod_python. One thing that I wasn't
> prepared for was dealing with persistence. I have an app that I'm
> trying to make work through mod_python, but everything goes to hell
> the moment there's multiple users.
>
> The way it's supposed to work is like this:
>
> The page is accessed. A number of variables are loaded. The user
> changes some of the variables, the changes are saved to a pickled
> file. The next time the user loads the page, those saved variable
> values are loaded back in.
>
> The problem is, as soon as the variables have been changed, if anyone
> accesses the page in the near future, they'll see those changed
> variables. Here's where it really starts driving me insane: a lot of
> the time, that second user will see the changed variables even if he
> has his own pickled file that has been loaded and was supposed to
> overwrite them.
>
> For example:
>
> I have a file with the variable "x = 0". A user changes the value to
> "x = 5", and that change is saved to a pickled file (tied to a session
> ID).
>
> A second user accesses the page, where the value is now "x = 5". He
> changed the value to "x = 10". This is saved to his own pickled file.
>
> The first user returns to the site. Since he's returning, his pickled
> file, with "x = 5", is loaded. Except, when he looks at the data, he
> sees "x = 10", even though that variable was supposed to have been
> changed to "5" when his file loaded.
>
> A more direct code example:
>
> The module lab.game has the class InitGame, which contains "daemons =
> {}". There's an instance of this class called "settings".
>
> A user runs the code, resulting in some values in "daemons":
> "{'berry2': 3, 'berry3': 5, 'berry1': 7}". These are pickled. The next
> user runs the code. I put this in to make sure "daemons" is getting
> reset:
>
>        req.write(str(lab.game.settings.daemons))
>        del lab.game.settings
>        try: req.write(str(lab.game.settings.daemons))
>        except: req.write("failed")
>        lab.game.settings = lab.game.InitGame()
>        req.write(str(lab.game.settings.daemons))
>
> Okay, that should wipe out any of the values and leave us with a clean
> slate, right? But no, it doesn't. This is the output:
>
>        {'berry2': 3, 'berry3': 5, 'berry1': 7}failed{'berry2': 3,
> 'berry3': 5, 'berry1': 7}
>
> So, even if I try deleting the variable itself, the moment I try to
> reinitialize it, the old values return.
>
> Does anyone know what I'm doing wrong, or if there's any way I can
> just kill mod_python's persistence altogether?

Apache is a multiprocess web server and if using worker MPM each
process can be multithreaded as well. It sounds like your application
may not be dealing properly with one or the other. Read:

  http://www.dscpl.com.au/wiki/ModPython/Articles/TheProcessInterpreterModel

Is the data you are changing global? Are you expected global data to
be the same between requests?

See summary in that document about shared data.

The provide more details about where these variables are that you are
changing. Ie., what context, global, local, session etc.

Graham



More information about the Mod_python mailing list