[mod_python] How to perform cleanup action when the python process dies?

Dan Eloff dan.eloff at gmail.com
Mon Jun 13 09:41:37 EDT 2005


Thanks Graham, I completely missed the fact that server is an object :)

-Dan

On 6/12/05, Graham Dumpleton <grahamd at dscpl.com.au> wrote:
> 
> On 12/06/2005, at 9:46 AM, Dan Eloff wrote:
> 
> > I have global resources that persist as long as the process does
> > (database connections) but they could also be files or sockets or
> > other resources that need to be released. Python destructors are very
> > dicey from what I've heard, there seems no guarantee that the __del__
> > method on the connection pool will be called before the database
> > module is itself destroyed. I can't use req.register_cleanup() since
> > the objects stay alive across all the requests. When apache is
> > restarted though, how do I ensure the database connections were closed
> > on exit?
> >
> > I saw a similar post to this on the list, but it just recommends not
> > to trust python destructors for important things like closing a
> > database connection.
> 
> Use req.server.register_cleanup(). Although accessed through the "req"
> object, it allows registration of a function which is to be called on
> server shutdown. Since you would only want to call it once, make sure
> you a keep a flag of your own which records when you have already done
> so.
> 
> Graham
> 
>



More information about the Mod_python mailing list