I am trying to understand the "daemon" flag of python thread. I know that
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.
But in my case, python program exits before daemon threads are left and the threads does not finish its work.
def ThreadDecorator(f):
def wrap(*args,**kargs):
t=Thread(target=f,args=args,kwargs=kargs)
t.daemon=True
t.start()
return wrap
@ThreadDecorator
def runSomething(*args,**kwargs):
i = 0
attente = 0
fileName=kwargs['file']
f=open(fileName,'wt')
while i < 50000:
f.write(str(i)+"\n")
# attente += 0.2
# attente += random.randint(1, 60) / 100
# time.sleep(attente)
i += 1
f.close()
return "Finished"
And main program
runSomething("5",file='test1.txt')
runSomething("6",file='test2.txt')
The first thread write only 5000 first integer while the second does not write any number
runSomething
) the daemon threads are killed/ended. – HollowayFalse
by default if they are spawned from a non daemon thread. – Holloway