In this piece of code, I expected to see a random element in the list go to 1 for a minimum of 5 seconds. Frequently I am seeing an element go to 1 for only a couple of iterations, then be reset to 0. What am I missing? Does it have to do with threads not shutting down? Can someone please tell me how to fix it?
import threading
import random
import time
def lreset(x):
global flags
flags[x] = 0
return
flags = [0,0,0,0,0,0,0,0]
while True:
index = random.randint(0,7)
flags[index] = 1
t = threading.Timer(5, lreset, [index])
t.start()
print flags
time.sleep(1)