[mod_python] server shutdown [was] i beg...

Graham Dumpleton grahamd at dscpl.com.au
Fri Aug 18 06:08:19 EDT 2006

On 18/08/2006, at 7:48 PM, Richard Lewis wrote:

> On Tuesday 15 August 2006 23:31, Graham Dumpleton wrote:
>> Richard Lewis wrote ..
>>> Do you mean that there will be no opportunity to have code run at  
>>> server
>>> shutdown?
>> Correct. Reason being that it doesn't actually work most of the time
>> anyway.
>> Because of how Apache is implemented, there is no reliable/safe  
>> way of
>> implementing this feature. If one can't do it properly, it seems  
>> better not
>> to attempt it at all.
> Goodness! So, in my mod_python applications I often acquire  
> database handles
> and store them in objects outside of the handler() function so that  
> they
> persist between requests. (This is to avoid the expense of  
> acquiring a new
> handle for every request).

And you can still do that.

> I then use a cleanup() function to release those
> database handles.
> The implication of this is that I will no longer be able to release  
> the
> handles at server shutdown, yes?

I think you miss what I have been saying. That it doesn't work now.  
If you
were to put calls to apache.log_error() in your cleanup handler and  
Apache when it is handling a decent amount of traffic, you will probably
find your cleanup functions aren't actually being called, or if they  
do, they
might not exit and will hang at some point. What you are more likely  
to see
in the Apache log is a series of SIGTERM signals being sent and then a
SIGKILL signal which is forcibly killing off the process.

> Can anyone suggest an alternative method of
> doing this? Are there any Python tricks where you can execute code  
> when the
> interpreter itself is about to stop?

Python does have means of calling code on application shutdown, but
because of how Apache uses signals to shutdown processes and how
the Apache main loop works, with the main loop being managed
by Apache and not Python they can't be used either.

> Or could I have a Python script running
> in another process which "looks in" to the mod_python process and
> periodically cleanly releases database handles?


In the greater scheme of things it shouldn't ultimately matter. This  
is because
your database server is going to notice that the connections to it  
have been
dropped and will cleanup the resources on its side anyway. It has to  
do this
as there is nothing to say that Apache or any client process connecting
to it will not simply crash without cleanly disconnecting. In other  
words, you
will not get resource leaks. That things didn't get cleanup in the  
Apache child
process doesn't matter as it has been killed anyway, with all its  
memory being
released back to operating system.


More information about the Mod_python mailing list