threads and exception in wxPython

Peter Hansen peter at
Thu Nov 4 15:44:17 CET 2004

(Sorry if duplicate: NTTP server screwed up.)

Peter Hansen wrote:
> Thomas Heller wrote:
>> # raise exception in thread
>> print pythonapi.PyThreadState_SetAsyncExc(t, id(exc))
> I should note for the record that neither Thomas'
> example above nor even my more complex one
> ... implements the required API fully.

A further addendum, or a caution/concern:

The docs also say that you should own the GIL when
you call this routine.  I suspect that in fact you
are supposed to have a lock on the GIL during the
_entire time_ that you are working with this routine,
specifically around all the following (pseudo)code:

n = PyThreadState_SetAsyncExc(id, Py_Object *)
if n > 1:
     PyThreadState_SetAsyncExc(id, NULL)

I suspect that the only reason the second call works
(supposedly it "reverts the effects" of the first call)
is because the interpreter has not had time to execute
any more bytecodes in the affected threads.  But since
the precise definition of this "reversion" is undocumented,
going to the source or an expert (which I'm clearly not)
would be the only way to sort out such "minor" issues...


More information about the Python-list mailing list