[mod_python] mod_python session trouble

Graham Dumpleton grahamd at dscpl.com.au
Tue May 10 07:11:14 EDT 2005


On 10/05/2005, at 8:33 PM, Wouter van Marle wrote:

>  Update on my own message:
>  I have changed the structure a bit, sess has now become req.session, 
> so I can pass around the session together with the request object 
> easily.
>  The locking problem has been solved by calling req.session.unlock() 
> every time after saving the session, however this causes the server to 
> start a new session for each request, and that's not the idea, as then 
> info gets lost.
>  Also I've noticed that writing and reading back of cookies does not 
> work properly. I'm trying to write a cookie, and when reading it back 
> in case of a new session I get the old data again. But that's for 
> another question.

I saving session in "req" anyway, do this:


>  def home(req):
>      sess = Session.Session(req)
>      if sess.is_new():
>          # get settings from cookies (using Cookie)
>      # page built up, using sess["language"] for the language setting.
>      sess.save()
>      return apache.OK

def home(req):
     if not hasattr(req,"session"):
       req.session = Session.Session(req)
     if req.session.is_new():
       ...
     ...
     req.session.save()
     return apache.OK

>  def english(req)
>      sess = Session.Session(req)
>      sess["language"] = "en"
>      sess.save()
>      home(req) # to redraw the main page

def english(req):
     if not hasattr(req,"session"):
       req.session = Session.Session(req)
     req.session["language"] = "en"
     return home(req)

Shouldn't need to unlock session then and same session object used.

Graham



More information about the Mod_python mailing list