81

I'm a little confused about what setting a thread to be a daemon means.

The documentation says this:

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. The initial value is inherited from the creating thread. The flag can be set through the daemon property.

I'm not sure what makes this different from a normal thread.

Is this saying that this program won't ever finish?

def threadfunc():
    while True:
        time.sleep(1)

threading.Thread(target=threadfunc).start()

Even though the main thread finishes it's execution. While will finish immediately?

def threadfunc():
    while True:
        time.sleep(1)

th = threading.Thread(target=threadfunc)
th.daemon = True
th.start()

I ask because I have a situation where in my main thread I'm calling sys.exit(), and the process just hangs and my other threads are running as I can see the log.

Does this have anything to do with sys.exit() being called with threads alive?

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
Falmarri
  • 47,727
  • 41
  • 151
  • 191

3 Answers3

53

Is this saying that this program won't ever finish?

Yes, that program won't finish, just try it out.

I ask because I have a situation where in my main thread I'm calling sys.exit(), and the process just hangs and my other threads are running as I can see the log. Does this have anything to do with sys.exit() being called with threads alive?

Yes, even exit won't stop other threads, it simply raises SystemExit in the main thread. So while the main thread will stop (just like it does on any other unhandled Exception), all other non-daemonic threads will continue to work.

Jochen Ritzel
  • 104,512
  • 31
  • 200
  • 194
  • Ok that's fine, but is there a way to make my program exit with threads running and still call atexit() hooks? – Falmarri Dec 02 '10 at 01:47
  • @Falmarri: What has `atexit` to do with it? Either turn your threads into daemons or write code that shuts them down, then your program can exit (and call the exit functions i presume). – Jochen Ritzel Dec 02 '10 at 14:00
  • I'm trying to hack something up to debug some code. I can't change the threads and stuff. I guess I'll have to look for another solution. – Falmarri Dec 02 '10 at 18:09
  • 1
    The documentation (https://docs.python.org/3/library/threading.html#thread-objects) says that "Daemon threads are abruptly stopped at shutdown. " - Doesn't this mean that - when the main program finishes the daemon threads are shutdown? – variable Jun 28 '21 at 05:25
20

Setting thread.daemon = True will allow the main program to exit. Apps normally wait till all child threads are finished before completing.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
TelegramSam
  • 2,770
  • 1
  • 17
  • 22
  • 1
    But, if you don't use join() that threads can run aline, as zombie process... so that daemon option should be used with join(). Don't? – m3nda Oct 23 '15 at 02:25
  • 6
    That's not quite True @erm3nda. With a Thread there is not extra *process.* Remember that, without other measures, all Python programs are single processes. `.join()` will block the calling scope until the Thread ends, which, of course, can be a problem if it won't ever end. Configuring a Thread to be daemonic just tells the parent Thread to kill it when it needs to rather than implicitly `.join()`ing when the parent wants to exit. I literally just learned this seconds ago and, for some reason, it was very counterintuitive to me. – John Carrell Sep 13 '18 at 14:20
  • Thanks @JohnCarrell – m3nda Sep 14 '18 at 18:27
  • The documentation (https://docs.python.org/3/library/threading.html#thread-objects) says that "Daemon threads are abruptly stopped at shutdown. " - Doesn't this mean that - when the main program finishes the daemon threads are shutdown? – variable Jun 28 '21 at 05:25
7
th.daemon = True #set this thread as a Daemon Thread

You can think in a Daemon thread as a service this means that it will be running in the background of your computer doing differents task, like indexing files, parsing xml, retrieving news etc, anything that is a long running process.

Your Main thread will finish and your daemon will still be running in the background, that is the reason why your program aka Main thread finish, if you want just put an infinite loop and you will see your thread still running. An example for a daemon thread is the garbage collection.

Alejandro Serret
  • 1,139
  • 15
  • 16
  • 5
    _"Your Main thread will finish and your daemon will still be running in the background"_ - isn't that exactly the case when the thread it _not_ a daemon? – Jeppe Aug 11 '20 at 09:36
  • are you saying if any thread is executing the `while true:` function, then that is a foreground thread, not a daemon one? – y_159 Apr 10 '21 at 16:27