Thread: thread killing

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    21
    Rep Power
    0

    thread killing


    I programmed this graphical interface with Tkinter, and in order to have it operational all the time I introduced threads in it.
    But if you quit the interface the other threads seem to be running still.
    How would you kill all the threads before quiting the interface? Or is there a "clean" way of doing things that resolves this problem automatically (I admit I am more of a python mac guyver but not as good, trying to put things together with my swiss knif)

    Thanks
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Originally Posted by winwin
    I programmed this graphical interface with Tkinter, and in order to have it operational all the time I introduced threads in it.
    But if you quit the interface the other threads seem to be running still.
    How would you kill all the threads before quiting the interface? Or is there a "clean" way of doing things that resolves this problem automatically (I admit I am more of a python mac guyver but not as good, trying to put things together with my swiss knif)

    Thanks
    There is no direct way of killing a thread in Python. One approach is to set a flag that is tested by the thread code periodically. Basically, you ask the thread to terminate and then hope that it does. The main thread should wait on an a threading.Event - if you set a timeout period then you can test to see if the flag is-set-or-not after the wait terminates. Dependent on the result you can modify your program exit strategy.

    Here was an interesting googled example:
    http://aspn.activestate.com/ASPN/Coo...n/Recipe/65222

    grim
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    21
    Rep Power
    0
    Yeah thanks it works quite okay! Not that easy i my opinion, but it works all the same (i would have prefered something like the good old signals I used when i did some C programing that kill anything anytime...)

    I'm getting to where I want to go, but I saw something very strange and I have trouble explaining it:

    Code:
    root = Tk()
    event1 = threading.Event()
    event1.set()
    I have two problems in fact:
    - when I put the event after the root creation, everything doesn't quit. The first thread to make the flag test does quit but then it seems the root.mainloop() starts again before all the threads do their flag test, and so the flag is set to True again...

    - when i put it before, then my sub threads see the event as False as soon as they are started... so they quit right away (they do this test one in a while:
    Code:
        if not event1.isSet():
                break
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    I would normally assume that the Tkinter thread is the main thread from which you launch the other worker threads. In which case I don't see why your root.mainloop is being called again.

    What you have posted tells me nothing I am afraid - I need more detail/code.

    grim

IMN logo majestic logo threadwatch logo seochat tools logo