Python thread daemon property

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

def threadfunc():
    while True:
        time.sleep(1)

th = threading.Thread(target=threadfunc)
th.daemon = True
th.start()

will finish immediately?

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?

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.


Need Your Help

SSL_CTX_use_PrivateKey_file fail under Linux

c++ linux openssl

I'm trying to use the SSL_CTX_use_PrivateKey_file function in OpenSSL under Linux, but it returns false. The surrounding code has been ported from Windows, where everything runs fine. Is there some...

Is it expected that all the units of a Project Group in Delphi 7 to be in one folder?

delphi standards packages

Maybe this applied to other Delphi's (I've only used 7). We've got our code broken up so that nearly every DLL in our fairly massive app is in a different folder.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.