Meaning of daemon property on Python Threads
Solution 1
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
Setting thread.daemon = True
will allow the main program to exit. Apps normally wait till all child threads are finished before completing.
Solution 3
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.
Falmarri
Updated on July 06, 2021Comments
-
Falmarri almost 3 years
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?
-
Falmarri over 13 yearsOk that's fine, but is there a way to make my program exit with threads running and still call atexit() hooks?
-
Jochen Ritzel over 13 years@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). -
Falmarri over 13 yearsI'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.
-
m3nda over 8 yearsBut, 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?
-
John Carrell over 5 yearsThat'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. -
m3nda over 5 yearsThanks @JohnCarrell
-
Jeppe almost 4 years"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?
-
y_159 about 3 yearsare you saying if any thread is executing the
while true:
function, then that is a foreground thread, not a daemon one? -
variable almost 3 yearsThe 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 almost 3 yearsThe 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?