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

Graham Dumpleton grahamd at dscpl.com.au
Sun Jun 12 19:33:20 EDT 2005


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