threads and loops

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • python-list

    threads and loops

    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.i sAlive():
    counterThread.j oin()
    else:
    counterThread.s tart()

    def tStop():
    tEvent.clear()

    counterThread=t hreading.Thread (target=counter , args=(1,))
    tEvent=threadin g.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\pytho n\lib\lib-tk\Tkinter.py", line 1345, in __call__
    return self.func(*args )
    File "C:\myweb\pytho n\SNC\Script1.p y", line 14, in tStart
    counterThread.s tart()
    File "C:\myweb\pytho n\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

  • Aahz

    #2
    Re: threads and loops

    In article <mailman.485.10 74468980.12720. python-list@python.org >,
    python-list <python-list@frenchfrie dhell.com> wrote:[color=blue]
    >
    >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:[/color]

    Rather than answering your question directly, I'll tell you to look at
    my slides at http://www.pythoncraft.com/OSCON2001/
    --
    Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

    A: No.
    Q: Is top-posting okay?

    Comment

    Working...