Python daemon threads

After reading: http://pymotw.com/2/threading/#daemon-vs-non-daemon-threads I expect the following code to complete in 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, he continues to print numbers forever. Am I missing something? I am on win7. I get the same behavior from windows shell and inaction.

+4
source share
1 answer

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

+4
source

Source: https://habr.com/ru/post/1496113/


All Articles