threads and exception in wxPython
Thomas Heller
theller at python.net
Wed Nov 3 12:50:05 EST 2004
Peter Hansen <peter at engcorp.com> writes:
> Peter Hansen wrote:
>> So the exception managed to get to the new thread, even though
>> I called SetAsyncExc *before* the thread was even created.
>> Now _that's_ what I call "asynchronous". ;-)
>
> Actually, it's what you call pilot error. I was retrieving
> threading._get_ident() in the thread initializer *before*
> the thread was even started... stupid pilot.
>
> Here's the final working version:
>
> >>> class T(threading.Thread):
> ... def __init__(self):
> ... threading.Thread.__init__(self)
> ... self.stopped = False
> ... self.start()
> ... def run(self):
> ... self.id = threading._get_ident()
> ... try:
> ... while not self.stopped:
> ... time.sleep(0.1)
> ... except:
> ... print 'thread terminated!'
> ...
> >>> t = T()
> >>> t
> <T(Thread-7, started)>
> >>> asyncexc = ctypes.pythonapi.PyThreadState_SetAsyncExc
> >>> exc = ctypes.py_object(ValueError)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> >>> asyncexc(t.id, exc)
I'm not sure this is the way to do it (but py_object is completely
undocumented, so far, and I don't rememeber the details myself). But,
it is fine to pass the id() of a python object, which is an integer
specifying it's address, when the function expects a PyObject*:
asyncexc(t.id, id(ValueError(42)))
> 1
> >>> thread terminated!
>
> >>> t
> <T(Thread-7, stopped)>
>
>
> -Peter
Thomas
More information about the Python-list
mailing list