How to terminate a thread when main program ends?

facha picture facha · Apr 2, 2010 · Viewed 156.9k times · Source

If I have a thread in an infinite loop, is there a way to terminate it when the main program ends (for example, when I press Ctrl+C)?

Answer

ʇsәɹoɈ picture ʇsәɹoɈ · Apr 2, 2010

If you make your worker threads daemon threads, they will die when all your non-daemon threads (e.g. the main thread) have exited.

http://docs.python.org/library/threading.html#threading.Thread.daemon