[Tutor] Threads and loops

Stryder python-list at frenchfriedhell.com
Fri Jan 16 10:41:55 EST 2004



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
############

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: 2336 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20040116/965485a8/winmail-0001.bin


More information about the Tutor mailing list