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

Chris McDonough chrism at plope.com
Wed Feb 22 12:16:20 CET 2012


On Wed, 2012-02-22 at 09:06 +1100, Graham Dumpleton wrote:
> If you want to be able to control a thread like that from an atexit
> callback, you need to create the thread as daemonised. Ie.
> setDaemon(True) call on thread.
> 
> By default a thread will actually inherit the daemon flag from the
> parent. For a command line Python where thread created from main
> thread it will not be daemonised and thus why the thread will be
> waited upon on shutdown prior to atexit being called.
> 
> If you ran the same code in mod_wsgi, my memory is that the thread
> will actually inherit as being daemonised because request handler in
> mod_wsgi, from which import is trigger, are notionally daemonised.
> 
> Thus the code should work in mod_wsgi. Even so, to be portable, if
> wanting to manipulate thread from atexit, make it daemonised.
> 
> Example of background threads in mod_wsgi at:
> 
> http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode#Monitoring_For_Code_Changes
> 
> shows use of setDaemon().
> 
> Graham

I've read all the messages in this thread and the traffic on the bug
entry at http://bugs.python.org/issue14073 but I'm still not sure what
to tell people who want to invoke code "at shutdown".

Do we tell them to use atexit?  If so, are we saying that atexit is
sufficient for all user-defined shutdown code that needs to run save for
code that needs to stop threads?

Is it sufficient to define "shutdown" as "when the process associated
with the application exits"?  It still seems to not necessarily be
directly correlated.

- C




More information about the Web-SIG mailing list