0

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

Ado Riadh
  • 19
  • 7
  • The quote you added explains the behaviour. When then main thread ends (the one calling `runSomething`) the daemon threads are killed/ended. – Holloway Nov 02 '16 at 09:34
  • So I have to mark them as False? – Ado Riadh Nov 02 '16 at 09:39
  • If you want them to keep running until completion, yes, although they are `False` by default if they are spawned from a non daemon thread. – Holloway Nov 02 '16 at 09:40
  • See the paraphrasing of what's in the documentation about daemon threads in [this answer](http://stackoverflow.com/a/38805873/355230). – martineau Nov 02 '16 at 10:01

1 Answers1

0

I feel the main block should contain a pool. Once all the threads are complete, you can call the pool.close() which ensures that all the threads have completed execution.

from multiprocessing import Pool
from datetime import datetime

pool = Pool(processes = 2)
num_workers = 2
curr_worker = 1
val = 5
if (curr_worker < num_workers ):
    file_name = 'test'+str(curr_worker)+'.txt'
    pool.apply_async(run_Something,args=(val,file_name,)))
    curr_worker += 1
    val += 1
else:
    pool.close()
    pool.join()
    curr_worker = 1
    val = 5
print ("Workers returned result", datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

The main exits after initiating the daemon threads. Since the daemon threads stop on the termination of the non-daemon main, we see that only test1.txt has 5000 records.

tasks for main is to initiate the below, post which it will exit:

runSomething("5",file='test1.txt')
runSomething("6",file='test2.txt')
ForeverLearner
  • 1,901
  • 2
  • 28
  • 51