[Tutor] Threads and loops

firephreek firephreek at earthlink.net
Wed Jan 14 16:08:28 EST 2004


Happy Thursday to all,

I'm starting to work with threads, but I'm a little confused.  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
############

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

-Stryder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: winmail.dat
Type: application/ms-tnef
Size: 2304 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20040114/ab9c50ce/winmail.bin


More information about the Tutor mailing list