[Web-SIG] A 'shutdown' function in WSGI
graham.dumpleton at gmail.com
Tue Feb 21 23:06:46 CET 2012
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:
shows use of setDaemon().
On 22 February 2012 00:46, Tarek Ziadé <ziade.tarek at gmail.com> wrote:
> On Tue, Feb 21, 2012 at 1:43 PM, Antoine Pitrou <solipsis at pitrou.net> wrote:
>> Tarek Ziadé <ziade.tarek at ...> writes:
>> > On Tue, Feb 21, 2012 at 10:24 AM, Graham Dumpleton
>> <graham.dumpleton at gmail.com> wrote:
>> > ...
>> > > But I don't think you can guarantee that everything is still up in
>> > > memory by
>> > > the time atexit gets called,
>> > > so you can't really call cleanup code there.
>> > The only thing which is done prior to atexit callbacks being called is
>> > waiting on threads which weren't marked as daemonised.
>> > which can lead to completely lock the shutdown if a lib or the program
>> > has a
>> > thread with a loop that waits for a condition.which it is not the case
>> > with
>> > signals, since you get a chance to properly stop everything beforehand.
>> That's a buggy lib or program. This has nothing to do with WSGI really.
> No, that has to do with : please let me clean my program before you try to
> kill it because I can't use signals :)
>> snippet Graham showed is run at any interpreter shutdown, even when you
>> run "python" in your shell.
> here's a very simple demo: http://tarek.pastebin.mozilla.org/1489505
> Run it with plain python, and try to ctrl-C it. You won't reach atexit and
> will get locked.
> (here: python 2.7 / mac os)
> If you use signals instead of atexit, you'll have it working.
> And this pattern (a thread in the background) is pretty common -- unless I
> am missing something here
>> Web-SIG mailing list
>> Web-SIG at python.org
>> Web SIG: http://www.python.org/sigs/web-sig
> Tarek Ziadé | http://ziade.org
> Web-SIG mailing list
> Web-SIG at python.org
> Web SIG: http://www.python.org/sigs/web-sig
More information about the Web-SIG