threads

David Bolen db3l at fitlinxx.com
Wed May 23 16:39:02 EDT 2001


"lrobb" <lrobb at home.com> writes:

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

Python will do an explicit .join() operation on every thread object
that is alive as it tries to exit, except for those that are marked as
"daemon" threads.

There are two main possibilities:

* Provide a mechanism to notify your timer thread that it should exit (the
  thread itself has to exit, it can't be externally killed) and use it
  when you are stopping.

* Declare the thread to be a daemon (use the setDaemon Thread method) when
  it starts and then Python will ignore it on exit and leave it up to the
  OS to clean up (which is probably just fine in your case since it
  doesn't sound like the thread is managing resources that you need to
  wind down "nicely" or anything)

> According to the threading docs, I can't :
> "Python's Thread class supports a subset of the behavior of Java's Thread
> class; currently,
> there are no priorities, no thread groups, and threads cannot be destroyed,
> stopped,
> suspended, resumed, or interrupted"
> 
> Whats up with that?

You can't externally kill a thread (there have been various
discussions here about that, but it's generally not a good idea
because it can lead to inconsistent process state) but you can have a
thread do any of these things to itself.

Typically, a mechanism is implemented (via events, or queues, or some
other communication scheme) by which a thread is notified from the
main application that it should (stop, suspend, resume, etc...).

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list