I have a python thread which should display a message every second, while the rest of the script continues running. But the timer.cancel
function does not work, as it still keeps running even after the main loop have been terminated:
from threading import *
import time
import sys
a = 0
def x():
while(True):
global a
print
print 'Beep', a
print
time.sleep(1) # wait 1 second
t = Timer(1.0, x) # create thread
t.start() # start thread
while True:
a = a + 1
print a
time.sleep(0.1)
if a > 30:
t.cancel()
if a > 50:
sys.exit()
What am I doing wrong ?