You miss a global declaration of thread1
in test
function:
def test():
global thread1
...
Otherwise, python treats thread1
in test
as a local variable, so in main thread1
is not seen as being initialized.
I would suggest though a different approach (that I find more clear and safer):
import time
import threading
count = 0
class Screen(threading.Thread):
def __init__(self, count):
threading.Thread.__init__(self)
self.count = count
def do_something(self):
while True:
time.sleep(1)
self.count += 1
if self.count >=3:
print "Stop!"
break
def run(self):
self.do_something()
def test():
thread1 = Screen(count)
thread1.start()
test()