I am a newbie in python programming, what I understand is that a process can be a daemon, but a thread in a daemon mode, I couldn't understand the usecase of this, I would request the python gurus to help me in understanding this.
-
1The best answer to this is http://stackoverflow.com/a/190017/260122 . – clacke Apr 11 '16 at 12:21
-
2Possible duplicate of [Daemon Threads Explanation](http://stackoverflow.com/questions/190010/daemon-threads-explanation) – clacke Apr 11 '16 at 12:23
-
hue: The `threading` documentation says not to use the old [`setDaemon()`](https://docs.python.org/3/library/threading.html#threading.Thread.setDaemon) method (although it doesn't say it use is deprecated). – martineau Nov 27 '18 at 18:22
-
It is not old, it has been added as an option when you create the thread as an argument so now you haven't have to use the setter. `class threading.Thread(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)` – K. Stopa Dec 06 '18 at 18:20
-
@martineau now it is deprecated (as of 2023): "DeprecationWarning: setDaemon() is deprecated, set the daemon attribute instead" – Michael Currie May 01 '23 at 11:01
5 Answers
Here is some basic code using threading:
import Queue
import threading
def basic_worker(queue):
while True:
item = queue.get()
# do_work(item)
print(item)
queue.task_done()
def basic():
# http://docs.python.org/library/queue.html
queue = Queue.Queue()
for i in range(3):
t = threading.Thread(target=basic_worker,args=(queue,))
t.daemon = True
t.start()
for item in range(4):
queue.put(item)
queue.join() # block until all tasks are done
print('got here')
basic()
When you run it, you get
% test.py
0
1
2
3
got here
Now comment out the line:
t.daemon = True
Run it again, and you'll see that the script prints the same result, but hangs.
The main thread ends (note that got here
was printed), but the second thread never finishes.
In contrast, when t.daemon
is set to True
, the thread t
is terminated when the main thread ends.
Note that "daemon threads" has little to do with daemon processes.

- 842,883
- 184
- 1,785
- 1,677
-
2
-
9@GreenAsJade: [t.setDaemon(True)](http://docs.python.org/2/library/threading.html#threading.Thread.setDaemon) is part of the old API. Nowadays, [t.daemon = True](http://docs.python.org/2/library/threading.html#threading.Thread.daemon) is the recommended way to make `t` a daemon thread. – unutbu Dec 30 '12 at 11:58
-
But why is it called "daemon threads" when it "has little to do with [daemon processes](http://en.wikipedia.org/wiki/Daemon_%28computer_software%29)." – laike9m Aug 01 '16 at 07:34
It looks like people intend to use Queue to explain threading, but I think there should be a much simpler way, by using time.sleep()
, to demo a daemon thread.
Create daemon thread by setting the daemon
parameter (default as None):
from threading import Thread
import time
def worker():
time.sleep(3)
print('daemon done')
thread = Thread(target=worker, daemon=True)
thread.start()
print('main done')
Output:
main done
Process finished with exit code 0
Remove the daemon argument, like:
thread = Thread(target=worker)
Re-run and see the output:
main done
daemon done
Process finished with exit code 0
Here we already see the difference of a daemon thread:
The entire Python program can exit if only daemon thread is left.
isDaemon()
and setDaemon()
are old getter/setter API. Using constructor argument, as above, or daemon
property is recommended.

- 3,847
- 30
- 32
In simple words...
What is a Daemon thread?
- daemon threads can shut down any time in between their flow whereas non-daemon (i.e. user threads) execute completely.
- daemon threads run intermittently in the background as long as other non-daemon threads are running.
- When all of the non-daemon threads are complete, daemon threads terminate automatically (no matter whether they got fully executed or not).
- daemon threads are service providers for user threads running in the same process.
- python does not care about daemon threads to complete when in running state, NOT EVEN the finally block but python does give preference to non-daemon threads that are created by us.
- daemon threads act as services in operating systems.
- python stops the daemon threads when all user threads (in contrast to the daemon threads) are terminated. Hence daemon threads can be used to implement, for example, a monitoring functionality as the thread is stopped by the python as soon as all user threads have stopped.
In a nutshell
If you do something like this
thread = Thread(target=worker_method, daemon=True)
there is NO guarantee that worker_method
will get executed completely.
Where does this behaviour be useful?
Consider two threads t1
(parent thread) and t2
(child thread). Let t2
be daemon. Now, you want to analyze the working of t1
while it is in running state; you can write the code to do this in t2
.
Reference:

- 30,962
- 25
- 85
- 135

- 2,374
- 1
- 30
- 33
Module Queue has been renamed queue starting with Python3 to better reflect the fact that there are several queue classes (lifo, fifo, priority) in the module. so please make the changes while using this example

- 41
- 2
I've adapted @unutbu's answer for python 3. Make sure that you run this script from the command line and not some interactive environment like jupyter notebook.
import queue
import threading
def basic_worker(q):
while True:
item = q.get()
# do_work(item)
print(item)
q.task_done()
def basic():
q = queue.Queue()
for item in range(4):
q.put(item)
for i in range(3):
t = threading.Thread(target=basic_worker,args=(q,))
t.daemon = True
t.start()
q.join() # block until all tasks are done
print('got here')
basic()
So when you comment out the daemon line, you'll notice that the program does not finish, you'll have to interrupt it manually. Setting the threads to daemon threads makes sure that they are killed once they have finished.
Note: you could achieve the same thing here without daemon threads, if you would replace the infinite while loop with another condition:
def basic_worker(q):
while not q.empty():
item = q.get()
# do_work(item)
print(item)
q.task_done()

- 983
- 9
- 23
-
Making `Thread`s daemonic does not make sure they are killed once they finish—it allows them to be killed whether they're finished or not. To make sure one's finished, you would need to call its [`join()`](https://docs.python.org/3/library/threading.html#threading.Thread.join) method. – martineau Nov 27 '18 at 18:37