<br><br><div class="gmail_quote">On 29 March 2012 20:48, Andrew Svetlov <span dir="ltr"><<a href="mailto:andrew.svetlov@gmail.com">andrew.svetlov@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I propose to add Thread.interrupt() function.<br>
<br>
th.interrupt() will set a flag in ThreadState structure.<br>
<br>
When interpreter switches to next thread it will check that flag.<br>
If flag is on then ThreadInterruptionError will be raised in thread context.<br>
If thread has blocked via threading locks (Lock, RLock, Condition,<br>
Semaphore etc) — exception is raised also.<br></blockquote><div><br></div><div><br></div><div>I've worked with .NET where you can interrupt threads and it was very useful. There is a complication though, if a thread is interrupted inside a finally block then vital resource cleanup can be interrupted. The way .NET solves this is to never raise the interrupt exception inside a finally block. Once a finally block is completed a pending thread interrupt exception will be raised. </div>
<div><br></div><div>The normal response to requests like this is for people to suggest that the thread itself should check if it has been requested to stop - this is fine for fine grained tasks but not for very coarse grained tasks.</div>
<div><br></div><div>Michael Foord</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Of course we cannot interrupt thread if it has been locked by C<br>
Extension call or just waiting for blocking IO.<br>
But, I think, the way to force stopping of some thread can be useful<br>
and has no incompatibility effect.<br>
<br>
The standard way to stop thread is the sending some message which is<br>
the signal to thread for termination.<br>
Pushing None or sentinel into thread message queue for example.<br>
<br>
Other variants:<br>
— check 'interrupted' state explicitly by call<br>
threading.current_thread().interrupted() than do what you want.<br>
— do the same as boost.threading does: check state in direct<br>
interruption point and locks if interruption is enabled.<br>
<br>
BTW, we can disable interruption mechanic by default and use it only<br>
if switched on by threading.enable_interruption()<br>
<br>
What do you think?<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Thanks,<br>
Andrew Svetlov<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br><pre cols="72"><a href="http://www.voidspace.org.uk/" target="_blank">http://www.voidspace.org.uk/</a><br><br>May you do good and not evil<br>May you find forgiveness for yourself and forgive others<br>
May you share freely, never taking more than you give.<br>-- the sqlite blessing <a href="http://www.sqlite.org/different.html" target="_blank">http://www.sqlite.org/different.html</a></pre>
<br>