5

I was reading about daemon threads, and came across this SO page, the comment under the answer says:

But joining a demonized thread opens most likely a whole can of trouble! I'm now considering to remove the join() call in my little diagram for the daemon-thread

I understand what daemon threads are used for and why but, suppose you had to wait for a daemon thread to finish what it's doing, how would you do it if calling join() is considered a lot of trouble?

1 Answers1

9

If you want to be able to .join a thread, it's better to not make it a daemon. Daemon threads are for when you want a thread to do its thing and you're not too concerned about when or if it finishes.

The point of making daemon threads is that the program will exit when there are no non-daemon threads left alive.

From the threading docs:

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 or the daemon constructor argument.

You can actually call .join on daemon threads, but it's generally considered to be not good practice.

You could get a daemon thread to set an Event just before it finishes, which one or more other threads check, but it's simpler just to use a non-daemon thread and .join it.

An earlier version of this answer claimed that you can't .join a daemon thread. That's incorrect. I was getting mixed up with dummy threads. Sorry about that. :oops:

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • Explain a little more why you won't call `join()` when it's daemon, because it's going to die anyway, so waiting is pointless? –  Nov 19 '17 at 18:43
  • @SvetlanaRosemond I've changed my answer. Sorry for the confusion. :( – PM 2Ring Nov 19 '17 at 19:02
  • No Worries. Why is it bad practice? Because it defeats the purpose of what a daemon thread is and what it's supposed to do? Example, I'll be waiting for a thread, that in reality I don't need to wait for? –  Nov 19 '17 at 19:05
  • 1
    @SvetlanaRosemond Waiting for daemon threads can be a bit weird. There's a good explanation here: [Multithreading - Daemon threads & join method](http://www.bogotobogo.com/python/Multithread/python_multithreading_Daemon_join_method_threads.php) – PM 2Ring Nov 19 '17 at 19:15
  • upvoted so as per the definition, does making a redis subscriber thread as a daemon thread that listens infinitely make sense? basically i went through pubsub and i want to listen to messages infinitely on a redis subscriber inside a therad, i am not sure if I should make it daemon or not – PirateApp Mar 17 '19 at 09:42
  • 1
    @PirateApp Sorry, I know nothing about Redis. – PM 2Ring Mar 18 '19 at 00:45
  • Daemon threads don't cause the main program thread to wait for the output. Similarly, the non daemon threads dont cause the main program thread to wait for the output. Both can make the main program wait if the main program uses .join. So what exactly is use case of daemon? – variable Nov 06 '19 at 13:47
  • @variable What I said in the 1st 2 paragraphs. Use daemon threads when you want the program to be able to exit even though some daemon threads are still alive. With non-daemon threads, you'd need some way to ask those threads to finish. – PM 2Ring Nov 06 '19 at 13:54
  • With non daemon thread, why do I need way to ask them to finish? – variable Nov 06 '19 at 17:10
  • @variable Because they might run forever if you don't. The main thread can't force other threads to stop. If you have more questions on this topic, please ask a proper question. The comments are not designed for conversations like this. – PM 2Ring Nov 06 '19 at 17:29
  • 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
  • @variable Yes, daemon threads are stopped when the main program finishes. In other words, when all other threads are dead and the only live threads are daemons, the program exits. However, as that doc says, "Their resources (such as open files, database transactions, etc.) may not be released properly". And that is *not* good. – PM 2Ring Jun 28 '21 at 06:23
  • When non-daemon threads are running, and the main program doesn't use .join(), then will main program end or wait for non-daemon threads to finish? – variable Jun 28 '21 at 07:13
  • @variable You can (and should) write a short program to test that... – PM 2Ring Jun 28 '21 at 10:24