[Tutor] [Q] about threads ...waiting :(
Mon, 04 Feb 2002 10:47:07 -0800
> lonetwin <email@example.com> wrote:
> Hi everybody,
> I'm playing around with threads using python on linux. ....
> Now, if a KeyboardInterrupt exception is raised during the execution of
> doStuff(), how do I catch it, kill all the threads and exit the
> application, probably printing "Aieee ....I got killed" on the way ??
Yes, but... ;)
What's happening is that the KeyboardInterrupt is indeed interrupting whichever thread is active when it happens, but the other threads are all independent. You need some way of telling the other threads that there's been a problem, and they should close. Python doesn't have any
way to kill a thread from outside, so you'll have to ask each thread to shut down on its own.
Look into threading.event -- you'll want to create an event object, call it something like Interrupted, and then pass a reference to it into each thread as you create them. Then, in your KeyboardInterrupt, you'll want to Set() that event. Each thread will, at various places in
its processing, look at that event, and if it's set, they'll know that they need to shut down.
If you need any more pointers on how to use events, just let us know.