threads

Graham Ashton graham at coms.com
Wed May 23 09:47:19 EDT 2001


In article <T1wO6.7071$CD5.2628025 at news2.rdc2.tx.home.com>, "lrobb"
<lrobb at home.com> wrote:

> I have a global timer thread that cycles an event every 20 minutes.
> 
> When I stop the program via (Ctrl-C), this thread doesn't die.
> 
> How can I stop him?

I'm a total thread (and Python) newbie, so disregard this if anybody else
posts, but you could have a global variable (e.g. am_quitting) that is
shared between both threads that tells the timer thread when it's time to
shut down.

Catch the keyboard error in the main thread and update your global
variable to true. Then get your timer thread to check the value of that
variable to see if it should exit. The main thread perhaps ought to wait
until the timer thread has acknowledged that's it quitting by setting
am_quitting to something different again.

e.g.

  am_quitting = 0 # not pressed ctrl-C yet 
  am_quitting = 1 # have pressed ctrl-C, main thread waiting for... 
  am_quttting = 2 # timer thread sets this to say it's done and dusted

I've no clue though really, so that might not even be a good idea. Don't
forget to use mutexs to control access to the am_quitting variable (or
whatever you call it).

The thing I don't know how to do is quit a thread from within the thread,
but I'm sure that's dead easy (might just be sys.exit() or something...
best read the docs for that).

Graham.



More information about the Python-list mailing list