CherryPy provides a bus that allows you to add events to the web server process. It is specified pretty clearly and CherryPy recently made it available as a standalone package, Magicbus (https://bitbucket.org/cherrypy/magicbus/overview). Specifically it allows you to send events on different signals the main server process might get. You can also use it for a general bus within the app server, but at its most basic level, the goal was to make the stop/start/restart events easy to hook into.
I've found it to be really helpful for managing processes and wrote a simple supervisor-ish app called Dad using it (http://bitbucket.org/elarson/dad). HTH Eric -- Eric Larson On Monday, February 20, 2012 at 10:03 AM, Tarek Ziadé wrote: > Hello > > I need to be able to call a function when the web application shuts down > (SIGTERM/SIGINT) -- the use case is to stop a background thread. > > I am currently using signals because it seems to be the most clean way to do > this. atexit is much trickier since you don't know when it's going to get > called and you might try to call objects that were garbage collected unless > you hack something to keep references alive. > > But signals are also tricky beasts since you may compete with other code that > are listening to them. For instance mod_wsgi don't like apps that have signal > handlers. > > Anyways, the bottom line is that the cleanest way to do this -- as per Chris > McDonough idea, would be to introduce in the WSGI protocol a "shutdown" > function the servers would be obligated to call before exiting. > > I am not sure yet about its arguments, maybe a signum + frame or simply an > exit code... > > But how do you like the idea ? That would solve for me the problem of having > to deal differently here depending on if I am called with mod_wsgi or > gunicorn or xxx > > > Cheers > Tarek > > -- > Tarek Ziadé | http://ziade.org > _______________________________________________ > Web-SIG mailing list > Web-SIG@python.org (mailto:Web-SIG@python.org) > Web SIG: http://www.python.org/sigs/web-sig > Unsubscribe: http://mail.python.org/mailman/options/web-sig/eric%40ionrock.org _______________________________________________ Web-SIG mailing list Web-SIG@python.org Web SIG: http://www.python.org/sigs/web-sig Unsubscribe: http://mail.python.org/mailman/options/web-sig/archive%40mail-archive.com