[mod_python] Session persistence issues and interpereter wiedness...

Jim Gallacher jpg at jgassociates.ca
Sun Mar 11 11:26:41 EST 2007

Fred of UrlBit.Us wrote:
> On Friday 09 March 2007 15:43, Fred of UrlBit.Us uttered thusly:
>> I am having a problem with PSP that seems to be copying objects in the
>> vars list rather than using the originals.
> What I thought was "object duplication" wasn't at all -- actually, I am 
> seeing two problems (and I mis-read my log dumps).
> I am not wanting PSP to automatically handle the session object, since I want 
> to handle that myself outside of PSP. So I pass in the session object as a 
> part of a composition object variable to PSP space, so PSP will leave it 
> alone. Then I explicitly save the Session with session.save().
> My problem is that for whatever the reason, the session appears to not be 
> loading on the next query, even though the SID is still there and the 
> Session object report it's not new. I have confirmed that the save is saving 
> the modifications (by hex-dumping the contents of the DBM files, etc., but 
> they are not loading. No exceptions are generated either. I create the 
> session in the handler with Session(req).

Make sure you create your session object before sending *any* content 
back to the client. The response header for the session cookie is set 
when the session object is created. The first call to req.write(), 
either explicitly, or implicitly with PSP or publisher, causes the 
response headers to be sent. If you create your session after that it 
will be too late. Each new request will always seem to create a new session.

By way of illustration, this bit of code would exhibit the problem:

def handler(req):
     req.content_type = 'text/plain'
     req.write('stuff\n') # this forces the response headers to be sent
     sess = Session(req)  # oops, headers already sent
                          #  - client never sees the session cookie
     req.write('more stuff')
     return apache.OK

You can confirm that the session cookie is not getting set by looking at 
the response headers on the client.

> The 2nd issue I am seeing -- maybe related, but I doubt it -- is that the 
> interpreter for the virtual server seems to reload every so often, as I 
> notice modules and plugins being reloaded after so many queries. Is this 
> suppose to happen?

Am I to assume that your modules are not changing? If so, this could be 
related to your apache configuration. For prefork-mpm (worker-mpm will 
be similar), if MaxRequestsPerChild is > 0, then the child process will 
be killed and a new interpreter created when httpd spawns a new child 

Likewise, take a look at:

StartServers         5
MinSpareServers      5
MaxSpareServers     10

If the load spikes a number of new processes may be created, each 
getting a new interpreter. When the load falls again httpd will kill 
some off to somewhere between 5 and 10.

> Running Linux (32-bit), Apache 2.0.55, mod_python 3.3.1, Python 2.5


More information about the Mod_python mailing list