views:

461

answers:

1

What difference it makes when I set python thread as a Deamon, using thread.setDaemon(True) ?

+5  A: 

A daemon thread will not prevent the application from exiting. The program ends when all non-daemon threads (main thread included) are complete.

So generally, if you're doing something in the background, you might want to set the thread as daemon so you don't have to explicitly have that thread's function return before the app can exit.

For example, if you are writing a GUI application and the user closes the main window, the program should quit. But if you have non-daemon threads hanging around, it won't.

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

Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.

The entire Python program exits when no alive non-daemon threads are left.

FogleBird
Ok. Now I have the clear idea about daemon threads. Thanks buddy!
Vijayendra Bapte