3

A very simple script.

test.py

import temp
temp.start()

temp.py

import threading, time
f=open("output.txt","w")
def temp():
    for i in range(5):
        f.write(str(i))
        time.sleep(5)
    f.close()
def start():
    t=threading.Thread(target=temp)
    t.setDaemon(True)
    t.start()

I expected Daemon thread to complete as main process test.py exits immediately.But the daemon thread exits with the main and does not act like a daemon.Am i missing something basic here?

vks
  • 67,027
  • 10
  • 91
  • 124
  • 2
    Why would the thread 'complete'? The docs seem pretty clear "Daemon threads are abruptly stopped at shutdown. Their resources (such as open files, database transactions, etc.) may not be released properly. If you want your threads to stop gracefully, make them non-daemonic and use a suitable signalling mechanism such as an Event." – pvg Sep 08 '17 at 19:18
  • @martineau then the main process waits for thread to complete – vks Sep 08 '17 at 19:19
  • 1
    no, it's the shutdown of the program. 'daemon thread' basically means 'don't take this thread into account when shutting down the python runtime'. It just gets tossed. – pvg Sep 08 '17 at 19:20
  • No, again, I refer you to the docs which you should review at https://docs.python.org/3/library/threading.html "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. " If you have a non-daemon thread left, the program doesn't exit. It exits only when daemon (or no) threads are left. – pvg Sep 08 '17 at 19:25
  • related: [Daemon threads vs daemon processes](https://stackoverflow.com/q/57222905/9059420) – Darkonaut Jan 31 '20 at 10:38

2 Answers2

3

This is described in some detail in the python documentation at

https://docs.python.org/3/library/threading.html

The most relevant bits are:

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.

and

Daemon threads are abruptly stopped at shutdown. Their resources (such as open files, database transactions, etc.) may not be released properly.

The overloading of the term 'daemon' and the negation contortions can make this a bit confusing but it what it boils down to is: A python program exits only after all of its threads complete, except for daemon threads which are simply terminated if no other non-daemon threads are left. In your case, that means the program exits killing your daemon thread before it has a chance to do anything (or, conversely, does not exit until your thread completes, if you setDaemon(false)).

pvg
  • 2,673
  • 4
  • 17
  • 31
  • Since the docs say "...the daemonic property is inherited from the current thread" there's no need for the main thread to call `setDaemon(false)` in the OP's case. Besides `setDaemon()` is part of the **Old** getter/setter API, so just setting the property directly would the "modern" way to do it. – martineau Sep 09 '17 at 16:40
  • @martineau generally, there is not but that's what the poster did so that's what the answer contains (I just noticed the poster deleted the comments with that bit but that's why we're going on about `setDaemon`) – pvg Sep 09 '17 at 17:31
0

complementing pvg's nice answer, a possible solution for your question is to use join(), in your case:

t.join()

More about join in "what is the use of join() in python threading"

A nice guide explaining in a practical way can be found at: https://realpython.com/intro-to-python-threading/

xCovelus
  • 578
  • 1
  • 9
  • 20