Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of daemon property on Python Threads

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?

like image 510
Falmarri Avatar asked Dec 01 '10 23:12

Falmarri


People also ask

What is daemon in python thread?

The threads which are always going to run in the background that provides supports to main or non-daemon threads, those background executing threads are considered as Daemon Threads. The Daemon Thread does not block the main thread from exiting and continues to run in the background.

What is daemon thread mention its properties?

Daemon thread in Java is a low-priority thread that runs in the background to perform tasks such as garbage collection. Daemon thread in Java is also a service provider thread that provides services to the user thread.

What is daemon process Python?

Daemon processes in PythonTo execute the process in the background, we need to set the daemonic flag to true. The daemon process will continue to run as long as the main process is executing and it will terminate after finishing its execution or when the main program would be killed.


2 Answers

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.

like image 138
Jochen Ritzel Avatar answered Sep 20 '22 18:09

Jochen Ritzel


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

like image 25
TelegramSam Avatar answered Sep 17 '22 18:09

TelegramSam