[Python-ideas] Interrupting threads

Andre Roberge andre.roberge at gmail.com
Sun Jan 27 04:12:39 CET 2013


On Sat, Jan 26, 2013 at 11:03 PM, MRAB <python at mrabarnett.plus.com> wrote:

> I know that this topic has been discussed before, but I've added a new
> twist...
>
> It's possible to interrupt the main thread using KeyboardInterrupt, so
> why shouldn't it be possible to do something similar to a thread?
>

How about simply using http://code.activestate.com/recipes/496960/ ?

André



>
> What I'm suggesting is that the Thread class could support an
> 'interrupt' method, which would raise a ThreadInterrupt exception
> similar to KeyboardInterrupt.
>
> Actually, there's more to it than that because sometimes you don't want
> a section of code to be interrupted.
>
> So here's what I'd like to suggest:
>
> 1. There's a private thread-specific flag called 'interrupt_occurred'.
>
> 2. There's a private thread-specific flag called 'heeding_interrupt'.
>
> 3. There's a context manager called 'heed_interrupt'.
>
>
> About the context manager:
>
> 1. It accepts a bool argument.
>
> 2. On entry, it saves 'heeding_interrupt' and sets it to the argument.
>
> 3. It catches ThreadInterrupt and sets 'interrupt_occurred' to True.
>
> 4. On exit, it restores 'heeding_interrupt'.
>
> 5. On restoring 'heeding_interrupt', if it's now True and
> 'interrupt_occurred' is also True, it raises (or re-raises)
> ThreadInterrupt.
>
>
> Here are some examples which I hope will make things bit clearer
> (although it's still somewhat involved!):
>
>
> Example 1:
>
> with heed_interrupt(False):
>     # some code
>
> Behaviour:
>
> On entry, the context manager saves heeding_interrupt and sets it to
> False.
>
> If an interrupt occurs in "some code", interrupt_occurred will be set
> to True (because heeding_interrupt is currently False).
>
> On exit, the context manager restores heeding_interrupt. If
> heeding_interrupt is now True and interrupt_occurred is also True, it
> raises ThreadInterrupt.
>
>
> Example 2:
>
> with heed_interrupt(False):
>     # some code 1
>     with heed_interrupt(True):
>         # some code 2
>     # some code 3
>
> Behaviour:
>
> On entry, the outer context manager saves heeding_interrupt and sets it
> to False.
>
> If an interrupt occurs in "some code 1", interrupt_occurred will be set
> to True (because heeding_interrupt is currently False).
>
> On entry, the inner context manager saves heeding_interrupt and sets it
> to True.
>
> If an interrupt occurs in "some code 2" , ThreadInterrupt will be
> raised (because heeding_interrupt is currently True). The exception
> will be propagated until it's caught by the inner context manager,
> which will then set interrupt_occurred to True.
>
> (It's also possible that interrupt_occurred will already be True on
> entry because an interrupt occurred in "some code 1"; in that case, it
> may short-circuit, skipping "some code 2" completely.)
>
> On exit, the inner contact manager restores heeding_interrupt and
> restores it to False.
>
> If an interrupt occurs in "some code 3", interrupt_occurred will be set
> to True (because heeding_interrupt is currently False).
>
> On exit, the outer context manager restores heeding_interrupt and
> restores it. If heeding_interrupt is now True, it will raise
> ThreadInterrupt.
> ______________________________**_________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/**mailman/listinfo/python-ideas<http://mail.python.org/mailman/listinfo/python-ideas>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130126/f9255911/attachment.html>


More information about the Python-ideas mailing list