I'm running a TurboGears app with mod_wsgi and apache, using pymongo wrapped with Ming for persistence. To failproof and scale my app I was interested in using mongo's support for replica sets through a pymongo ReplicaSetConnection.
However, as specified in the above links, one must call close()
when done with every pymongo ReplicaSetConnection
object, otherwise it will leave a bunch of zombie processes floating around (wtf pymongo). This sort of behavior isn't supported very well in Ming (it actually doesn't really support Replica Sets yet), but I managed to write a cleanup function that closes all pymongo connections.
Now I want to run my cleanup function on process cleanup for all my app processes. These have two basic entry points: mod_wsgi through apache and paster commands. It is no problem to add my cleanup to the paster commands, but is there a way to specify a cleanup function for wsgi processes?
If not, any other advice for setting this up correctly?
You have no guarantee your code will even be called as processes can just crash or be forcibly exited before it is called. With that caveat, read the mod_wsgi documentation about this specific issue:
http://code.google.com/p/modwsgi/wiki/RegisteringCleanupCode