is it safe to stop a thread?

Parzival Herzog parz at home.com
Wed May 23 00:54:46 EDT 2001


"Dustin Boswell" <boswell at ugcs.caltech.edu> wrote in message news:3B0AFDEF.44E29573 at ugcs.caltech.edu...
> The "threading" module doesn't allow stopping, so that seems useless.
> But the "thread" module DOES

It DOES? How? the docs I have say that a thread can stop ITSELF, by
raising SystemExit, or calling sys.exit(). But nowhere
have I seen the ability for one thread to stop a
another thread. Please correct me if I'm wrong,
I would certainly be happy if I was wrong and
threads could be stopped.

Past posts on this group have suggested that the
subject thread should poll for an indication
that it should stop itself, and that one thread
trying to stop another thread from another is
a sign of poor design.

I would disagree, and ask if anyone can offer a
more rational explanation why Python threads
are designed to be not stoppable (if I am not
mistaken about this.)

Polling for a "stop" indication cannot be
done reliably for any thread using code not written
by the  programmer who wants to achieve a reliable stop:
The thread may block, or call some library code
that is not under the programmer's control that
fails to poll, or blocks without the programmer's
knowledge. Even for the code that is under the
programmers control, it can be difficult to
design arbitrary algorithms that always poll,
and such coding certainly is more difficult to
understand an maintain.

IMHO, it would be far better, if as in Modula-3,
one thread could "alert" another thread, thereby
raising an "alerted" exception in that thread, which
if not caught and otherwise dealt with would end that
thread.

- Parzival







More information about the Python-list mailing list