Jim Gallacher
jpg at jgassociates.ca
Tue Feb 27 14:26:07 EST 2007
David Kramer wrote: > Hi. I'm pretty new to mod_python. I'm porting a mod_python program > from Windows to Linux, and ran into the problem I posted a week or so > ago, where under Windows, all the variables automatically persisted (due > to Apache being one process under Windows), and the program was written > to expect that. I'm trying to store and recall the data using a > session, but I'm having some strange problems, and some questions. > > The biggest problem is that on occasion, when I call > sess = Session.Session(req) > the program just halts and never returns from that line. No errors in > /var/log/htttpd/error_log, and the page times out. What can cause that? To protect the integrity of your session data, the sessions are locked for the duration of the request. Usually when you get a deadlock it is the result of creating 2 (or more) session instances within the same request. For example, the following bit of code would deadlock: def handler(req): sess = Session.Session(req) stuff(req) return apache.OK def stuff(req): sess = Session.Session(req) req.write('stuff called') In the above code, the first session instance locks the session. The 2nd call to Session in stuff() blocks as the session is already locked, resulting in your deadlock. The usual recommendation is to make sure you only create one session instance per request, and pass that object around as an attribute of the request object. For example the above code would look like this: def handler(req): req.session = Session.Session(req) stuff(req) return apache.OK def stuff(req): sess = req.session req.write('stuff called') > I can often get the program working again by deleting any cookies > created by it, then restarting Apache, but that's not exactly > production-ready. > > I wrote a small test program that tries to create an instance of a small > class and store that in the session data, and that seems to work (though > I've never worked in a web evironment where you could store objects in > cookies without somehow serializing them yourself). You are not storing the data in a cookie. Only the session id is set in the cookie. The session data is stored on the server. Various backend stores are available, depending on your mod_python version and your OS. Regardless of the store used, the data is a python cPickle. > Is that really the > case? Is there some rule about what can be stored in sessions and what > cannot? For various reasons it's best to restrict your session data to simple types such as lists, tuples, dictionaries, strings, integers, etc. > Is there a way for me to see the contents of session data on the server? > I tried following the code in the Session module, but I'm not sure > where or in what format the data is stored. I'm guessing "dbm" is a > Berkeley database that gets stored in a file somewhere, so I should be > able to query it somehow. Yes it is a dbm file, with the session id as the key. For mod_python 3.1 the default location, as I recall, is /tmp/mp_sess.dbm > I read somewhere that the stored data is > pickled, True. > so I'll have to write a Python program to display it, which > shouldn't be a problem. Also true. db = dbm.open('/tmp/mp_sess.dbm', 'r') sess = db[session_id] > Lastly, I found the Session documentation "rather lacking" at > http://www.modpython.org/live/current/doc-html/pyapi-sess.html > Are there any mod_python tutorials or examples or other docs that could > help me? I would love to RTFM, but I think I need a better FM. Some people have been chipping away at some additional pages that you may find useful: http://wiki.apache.org/mod_python/CategoryExamples Jim
|