Meaning of daemon property on Python Threads

PythonMultithreadingDaemonPython Multithreading

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

> 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.

Solution 2 - Python

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

Solution 3 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFalmarriView Question on Stackoverflow
Solution 1 - PythonJochen RitzelView Answer on Stackoverflow
Solution 2 - PythonTelegramSamView Answer on Stackoverflow
Solution 3 - PythonAlejandro SerretView Answer on Stackoverflow