[Python-ideas] Interrupting threads
Charles-François Natali
cf.natali at gmail.com
Tue Jan 29 08:23:33 CET 2013
> The point has been made that you don't want an interruption in the
> middle of an exception handling routine. That's true. You also don't
> want an interruption in the middle of a 'finally' block.
That's a good start :-)
> I think the problem here is that most of what I've been talking about
> regarding the context manager actually belongs to the 'try' statement;
> context managers are, after all, built on the 'try' statement.
> [...]
Several points:
- I prefer the original "interruption" word to "cancellation":
interruption is the mechanism by which a thread is notified of an
asynchronous interruption/cancellation/whatever request. Cancellation
is one of the potential outcomes of a thread interruption: the thread
could ignore it, handle it in some specific way and continue its merry
life, *or* cancel its activity and bail out. Also, "interruption" is
already familiar to anybody knowing about hardware interrupts, and has
precedent in other languages (e.g. Java, C#. pthread has cancellation
points but those are really cancellation).
- I don't understand what would happen by default, i.e. outside of any
try/context manager: could an interruption exception be raised at any
point?
- I still don't see what this brings over a simple, explicit static
Thread.interrupted() method. Interrupting a thread (through
thread.interrupt()) would just set this flag (which would probably be
an event/atomic read/write variable to assure memory visibility), and
then a thread could just call Thread.interrupted() to check for
pending interruption, and react accordingly. You don't have to mess
with the 'heed' flag when an exception is raised, you're sure an
asynchronous exception won't pop up at an arbitrary point in the code,
it's simpler, and well, "explicit is better than implicit".
The only usage I can see of an interruption exception is, as in Java,
to interrupt a blocking call (which is currently not supported).
- Really, "heed"? I've never had to look up a word in a dictionary
while reading a technical book/presentation/discussion before. I may
not be particularly good in English, but I'm positive this term will
puzzle many non native speakers...
More information about the Python-ideas
mailing list