[Web-SIG] A 'shutdown' function in WSGI

Tarek Ziadé ziade.tarek at gmail.com
Mon Feb 20 17:03:49 CET 2012


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/web-sig/attachments/20120220/2840ccc5/attachment.html>


More information about the Web-SIG mailing list