I want to end a loop running in a separate thread using a global variable. but this code does not seem to stop the thread in loop. I expect the program not to print any more '.' after 2 seconds, but it still runs indefinitely.

Am I doing something fundamentally wrong here?

import time
import threading
run = True

def foo():
    while run:
        print '.',

t1 = threading.Thread(target=foo)
t1.run()
time.sleep(2)
run = False
print 'run=False'
while True:
    pass
Edit
Report