threads killing other threads

Skip Montanaro skip at pobox.com
Thu Feb 7 23:23:51 EST 2002


    Jeff> Create a global threading.Event object, named something
    Jeff> descriptive like Quit, and have every thread peek at it
    Jeff> occasionally.  If the Quit event is set, then the thread aborts.
    Jeff> If you need to be able to kill specific threads individually, then
    Jeff> you have a separate event object for each thread.

    Jeff> Of course, this *does* rely on the threads being well-behaved, and
    Jeff> not getting stuck in an infinite loop elsewhere, and so on....
    Jeff> but then, that's pretty much a requirement anyhow.

This note from the threading module docs suggests to me a different way to
do things:

    A thread can be flagged as a ``daemon thread''.  The significance of
    this flag is that the entire Python program exits when only daemon
    threads are left.

If all your worker threads are daemon threads, when your main thread exits,
the program should exit as well.  I haven't tried this, but may well try it
shortly.  I do have trouble getting a multi-threaded server to exit.

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)




More information about the Python-list mailing list