[mod_python] Sessions again...

Luis M. Gonzalez luismg at gmx.net
Thu Jan 19 17:49:07 EST 2006


Ok, Jim, I'll clarify the problem:
Both scripts initiate their sessions the same way and with the same name:

Script nr.1:

    s= Session.Session(req)
    if s.is_new():
       s['items'] = {}

Script nr.2:

    s= Session.Session(req)
    if s.is_new():
       s['counter'] = 0

I did it this way because I asume that for using both scripts in the same 
site, I should have only one session started.
(Note that I also tried starting different sessions for each script, with 
different names, but the problem persists).

Actually, I already fixed this problem (with a wacky work-around though...).
In each script, I created the session variable corresponding to the other 
script, as follows:

Script nr.1:

    s = Session.Session(req)
    if s.is_new():
        s['counter'] = 0
        s['items']= {} # workaround to avoid trouble with cart script

Script nr.2:

    s = Session.Session(req)
    if s.is_new():
        s['items']= {}
        s['counter'] = 0 # workaround to avoid trouble with recset script

Well, this fixed the problem in this case, but i don't think this woraround 
fits into mod_python's best practices...

Luis



> You'll need to clarify your problem a little, but I think you may be 
> assuming that you get a different session object for each script. That is 
> not the case, at least by default. You get one session object per browser 
> so if you want to use it in independent scripts, each script will need to 
> catch the KeyError exception or do some other test to see if you need to 
> initialize your session data.
>
> You can also do some thing like this:
>
> r.counter = sess.get('counter', 0)
> ...
> ...
> r.counter += 1
> sess['counter'] = r.counter
> sess.save()
>
> Jim
> 



More information about the Mod_python mailing list