[Python-3000] threading, part 2
Guido van Rossum
guido at python.org
Wed Aug 9 20:53:31 CEST 2006
On 8/9/06, Nick Coghlan <ncoghlan at gmail.com> wrote:
> That check is already there:
>
> int PyThreadState_SetAsyncExc( long id, PyObject *exc)
> Asynchronously raise an exception in a thread. The id argument is the
> thread id of the target thread; exc is the exception object to be raised. This
> function does not steal any references to exc. To prevent naive misuse, you
> must write your own C extension to call this. Must be called with the GIL
> held. Returns the number of thread states modified; if it returns a number
> greater than one, you're in trouble, and you should call it again with exc set
> to NULL to revert the effect. This raises no exceptions. New in version 2.3.
Note that it is intentionally not directly accessible from Python --
but this can be revised.
> In Python 2.5, you can use ctypes to get at the whole C API from Python code,
> and calling thread.get_ident() in the run() method will allow you to find out
> the thread id of your thread (you'll need to save that value somewhere so
> other code can get at it).
>
> All Tober is really asking for is a method on threading.Thread objects that
> uses this existing API to set a builtin ThreadExit exception. The thread
> module would consider a thread finishing with ThreadExit to be
> non-exceptional, so you could easily do:
>
> th.terminate() # Raise ThreadExit in th's thread of control
> th.join() # Should finish up pretty quickly
>
> Proper resource cleanup would be reliant on correct use of try/finally or with
> statements, but that's the case regardless of whether or not asynchronous
> exceptions are allowed.
I'm +0 on this.
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-3000
mailing list