threads and loops

P

python-list

Hello, I posted this to the tutor list, but didn't get any responses, unless my email client really messed up. So I'll try here.

I'm starting to work with threads, but I'm a little confused. I think I understand the concepts, but not the controls. Why doesn't something like this work:

#############
import threading

def counter(x):
while tEvent.isSet():
x+=1
print x

def tStart():
tEvent.set()
if counterThread.isAlive():
counterThread.join()
else:
counterThread.start()

def tStop():
tEvent.clear()

counterThread=threading.Thread(target=counter, args=(1,))
tEvent=threading.Event()

#####################

After that I have controls setup for a Tkinter box with two buttons, one has
tStart as it's command value, and the other tStop. When I run the program,
it starts fine, and then the loop stops when I press 'stop', but when I try
to press 'start' again, I get an error:

###########
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\myweb\python\lib\lib-tk\Tkinter.py", line 1345, in __call__
return self.func(*args)
File "C:\myweb\python\SNC\Script1.py", line 14, in tStart
counterThread.start()
File "C:\myweb\python\lib\threading.py", line 404, in start
assert not self.__started, "thread already started"
AssertionError: thread already started
############

Because my 'while' func depends on the flag, when the flag is false, and it stops, the thread should die, right?

And why the need for the extra comma in the args value when defining my
instance?

-Stryder
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top