python threading : cannot switch thread to Daemon
Question: I would expect next code to be executed simultaneously and all filenames from
os.walk iterations , that got 0 at random , will get in result dictionary. And
all threads that have some timeout would get into deamon mode and will be
killed as soon as script reaches end. However, script respects all timeouts
for each thread.

Why is this happening? Should it put all threads in backgroung and kill them
if they will not finish and return result before the end of script execution?
thank you.

    
    
    import threading
    import os 
    import time 
    import random
    
    def check_file(file_name,timeout):
        time.sleep(timeout)    
        print file_name 
        result.append(file_name)
    
    result = []
    for home,dirs,files in os.walk("."):
        for ifile in files :
            filename = '/'.join([home,ifile])
            t = threading.Thread(target=check_file(filename,random.randint(0,5)))
            t.setDaemon(True)
            t.start()
    
    print result
    

Solution: I found my mistake:

    
    
    t = threading.Thread(target=check_file(filename,random.randint(0,5)))
    

has to be

    
    
    t = threading.Thread(target=check_file, args=(filename,random.randint(0,5)))
    

In this case, threading will spawn a thread with function as object ang give
it arguments. In my initial example, function with args has to be resolved
BEFORE thread spawns. And this is fair.

However, example above works for me at 2.7.3 , but at 2.7.2 i cannot make it
working. I `m getting got exception that

    
    
    function check_file accepts exactly 1 argument (34 is given).
    

Soulution : in 2.7.2 i had to put ending coma in args tuple , considering that
i have 1 variable only . God knows why this not affects 2.7.3 version . It was

    
    
    t = threading.Thread(target=check_file, args=(filename))
    

and started to work with

    
    
    t = threading.Thread(target=check_file, args=(filename,))
    


Answer: I understand what you were trying to do, but you're not using the right format
for threading. I fixed your example...look up the Queue class on how to do
this properly.

Secondly, never ever do string manipulation on file paths. Use the `os.path`
module; there's a lot more than adding separators between strings that you and
I don't think about most of the time.

Good luck!

    
    
    import threading
    import os 
    import time 
    import random
    import Queue
    
    def check_file():
        while True:
            item = q.get()
            time.sleep(item[1])
            print item
            q.task_done()
    
    q = Queue.Queue()
    result = []
    for home,dirs,files in os.walk("."):
        for ifile in files:
            filename = os.path.join(home, ifile)
            q.put((filename, random.randint(0,5)))
    
    number_of_threads = 25
    for i in range(number_of_threads):
        t = threading.Thread(target=check_file)
        t.daemon = True
        t.start()
    
    q.join()
    
    
    print result
    

