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 On 22 February 2012 00:46, Tarek Ziadé <ziade.ta...@gmail.com> wrote: > > > On Tue, Feb 21, 2012 at 1:43 PM, Antoine Pitrou <solip...@pitrou.net> wrote: >> >> Tarek Ziadé <ziade.tarek@...> writes: >> > >> > >> > On Tue, Feb 21, 2012 at 10:24 AM, Graham Dumpleton >> <graham.dumple...@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 :) > > >> >> The >> snippet Graham showed is run at any interpreter shutdown, even when you >> simply >> 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 > > > Cheers > Tarek > >> >> >> Regards >> >> Antoine. >> >> >> _______________________________________________ >> 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/ziade.tarek%40gmail.com > > > > > -- > Tarek Ziadé | http://ziade.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/graham.dumpleton%40gmail.com > _______________________________________________ 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