0

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?

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
Will
  • 4,498
  • 1
  • 21
  • 20

2 Answers2

0

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

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • I believe this should be sufficient. My processes hang when I don't close the sessions, so I'm assuming the mentioned "background task" is in the main process. I'll confirm this here when I run some tests. – Will Aug 06 '12 at 20:48
  • The cleanup function would be called in each WSGI process where you call atexit.register(). – Graham Dumpleton Aug 07 '12 at 11:08
  • Isn't there something standard in WSGI itself for this? – Christopher Smith Oct 22 '14 at 16:28
  • In the standard for what in particular? Post request cleanup is covered in that document. For process wide cleanup on shutdown, no, the WSGI specification doesn't have anything. – Graham Dumpleton Oct 23 '14 at 21:18
0

TurboGears itself also provides the shutdown hook which can be registered using base_config.register_hook inside config/app_cfg.py.

The shutdown hook gets called whenever the process exists

amol
  • 1,771
  • 1
  • 11
  • 15