threads and loops

python-list python-list at frenchfriedhell.com
Sun Jan 18 23:38:07 CET 2004


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20040118/bea88dbe/attachment.html>


More information about the Python-list mailing list