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?

2

3 Answers 3

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.

4
  • Ok that's fine, but is there a way to make my program exit with threads running and still call atexit() hooks?
    – Falmarri
    Dec 2, 2010 at 1: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). Dec 2, 2010 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 2, 2010 at 18:09
  • 1
    The documentation (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, 2021 at 5:25
20

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

4
  • 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, 2015 at 2: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. Sep 13, 2018 at 14:20
  • Thanks @JohnCarrell
    – m3nda
    Sep 14, 2018 at 18:27
  • The documentation (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, 2021 at 5: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.

2
  • 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, 2020 at 9: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, 2021 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.