4

After reading: http://pymotw.com/2/threading/#daemon-vs-non-daemon-threads I expect following code to terminate after 2 seconds:

from threading import Thread
from time import sleep

def a():
    i = 0
    while 1:
            print i
            i+=1



t = Thread(target=a)
t.setDaemon(True)
t.run()
sleep(2)

However, it keeps printing numbers forever. Am I missing something here? I am on win7. I get same behaviour from windows shell and idle.

1 Answer 1

4

You should call t.start(), not t.run(). The first one will spawn a new thread and call run itself from there. Calling run on your own causes you to execute the a function in your current thread.

Sign up to request clarification or add additional context in comments.

1 Comment

Answer was much simpler than I thought :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.