[mod_python] Session management

Chris Jackson christopher.jackson at gmail.com
Fri Jan 14 13:31:56 EST 2005


This seems correct however, if in "func" I create a session {key:value}

...
e.g. sess['mykey'] = myvalue
sess.save()
...

Then, when I get to "otherFunc" and try to access sess['mykey'], 
I get a KeyError because it has created a new sess object.

The solution I'm trying to do now is pass around the sess.id() to each function,
and then create new sessions based on this sess.id()

sess = Session(req, sessid, None, 1800, 1)

Unless there is some other way so that I do not get KeyErrors.  Also
note that at the end of each function i'm using
req.internal_redirect(...). I don't know if the internal_redirect is a
problem or not.

~= Chris =~


On Fri, 14 Jan 2005 14:22:50 +0900, Hiroaki KAWAI <kawai at iij.ad.jp> wrote:
> Please don't put sess in global scope as Manfred Stienstra mentioned
> previously. That scope is module scope, one in one interpreter.
> 
> Every function that use session would be like this:
> 
> def func(req):
>         sess=Session(req) # you always need this line.
>         sess.load()
>         # do something
>         sess.save()
>         return "something"
> 
> The problem you encountered was:
> - send request to httpd
> - new python interpreter is created (httpd child process
>   is creatated)
> - your python module for Publisher Handler loaded.
> - call otherFunc at the first time.
> - if you drop "sess=Session(req)", sess.load() is passed, which means
>   NoneType type ojbect method call.
> 
> 
> > Hmm... the session object is still dropping (being lost) , the
> > recovered (being remember) from time to time.
> >
> > Brief view of the Publisher Handler setup.
> > *Note the sess variable is not visible to other functions unless
> > declared globally.
> >
> > [Global Space]
> > sess = None
> >
> > def login(req):
> >    global sess
> >    sess = Session(req)
> >    sess['this']  = 'that'
> >    ...<more code>...
> >    sess.save()
> >    return
> >
> > def otherFunc(req):
> >     global sess
> >     sess = Session(req)  # tested with and without this line (correct?)
> >     sess.load()
> >     ...<more code>...
> >     sess.save()
> >     return
> >
> > I can see the session being written to a file in a serialized way in /tmp
> >     mp_sess.dbm.bak
> >     mp_sess.dbm.dat
> >     mp_sess.dbm.dir
> >
> > I've also executed a:
> >
> >     watch -d 'ls -l /tmp/mp*'
> >
> > And upon creating new sessions...via separate browsers... I have seen the file
> > size fo the above files grow in real-time, which means that data is in
> > fact being added to the sessions which are partly human-readable.
> >
> > However, the problem I'm having is that occasionally when
> > transitioning to other pages such as http://mysite.com/otherFunc,
> >
> > will return 'NoneType' object has no attribute 'load'  # referring to
> > the sess object.
> >
> > So I click the [BACK] button in the browser and re-submit the data.
> > After about 7 times, the script all of a sudden recognizes the sess
> > object.  Sometimes this error does not happend and loads on the first
> > try. How can I prevent this behavior?
> >
> > ~= Chris =~
> >
> >
> > On Wed, 12 Jan 2005 23:48:39 +0900, Hiroaki KAWAI <kawai at iij.ad.jp> wrote:
> > > Of cource, most popular and easiest way is to use Session module.
> > > Session will store the information somewhere in 'session storage' that
> > > the module knows how to get it, and the actual mechanism is hidden.
> > > # i.e., inter-process communication via file or database.
> > >
> > > So, make sure that you save the session information to the session
> > > storage in each request processing as Ron says. ;-)
> > >
> > >
> > > Chris Jackson <christopher.jackson at gmail.com> wrote:
> > > > Ah, that makes sense...Now I just need to find which "inter-process"
> > > > technique I should be using in order to maintain consistency ;)
> > > >
> > > > ~= Chris =~
> > > >
> > > >
> > > > On Wed, 12 Jan 2005 12:55:47 +0900, Hiroaki KAWAI <kawai at iij.ad.jp> wrote:
> > > > > > > Depending on what type of apache configuration you run, one or more
> > > > > > > interpreters will be used. Sometimes interpreters die and new ones get
> > > > > > > created.
> > > > > >
> > > > > > Hmm...it's interesting you say that somtimes the interpreters die and
> > > > > > new ones get created because I'm running into a "problem" where I will
> > > > > > test a mod_python page and it will works perfectly several times again
> > > > > > and again, but then i'll test it out again, and the interpreter seems
> > > > > > to "forget" the values.  I'll keep testing the page with the same
> > > > > > values, and then the page will work again.
> > > > >
> > > > > One interpreter is created per one apache child process. If you're running
> > > > > prefork MPM (and set MaxRequestsPerChild or have multiple spare child
> > > > > servers), you're posting a HTTP request sometimes to the same interpreter,
> > > > > sometimes to other interpreter.
> > > > > I think one of 'inter-process' techniques is useful for what you're
> > > > > trying.
> > > > > # If I'm saying wrong, someone please correct. :-)
> > > > >
> > > > > ---Hiroaki Kawai
> > > > >
> > > > > _______________________________________________
> > > > > Mod_python mailing list
> > > > > Mod_python at modpython.org
> > > > > http://mailman.modpython.org/mailman/listinfo/mod_python
> > > > >
> > >
> > > ---Hiroaki Kawai
> > >
> > > _______________________________________________
> > > Mod_python mailing list
> > > Mod_python at modpython.org
> > > http://mailman.modpython.org/mailman/listinfo/mod_python
> > >
> 
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
>


More information about the Mod_python mailing list