[Python-Dev] PyThreadState_SetAsyncExc, PyErr_Clear and native extensions

Gabriel Becedillas gabriel.becedillas at gmail.com
Thu May 11 16:46:20 CEST 2006

I use PyThreadState_SetAsyncExc to stop a python thread but there are
situations when the thread doesn't stop and continues executing
normally. After some debugging, I realized that the problem is that
PyThreadState_SetAsyncExc was called when the thread was inside a
native extension, that for some reason calls PyErr_Clear. That code
happens to be inside boost::python.
I do need to stop the thread from executing Python code as soon as
possible (as soon as it returns from a native function is also
Because we have embedded Python's VM in our product, I'm thinking of
modifying PyErr_Clear() to return immediately if the thread was
stopped (we determine if the thread should stop using our own

void PyErr_Clear(void)
        if (!stop_executing_this_thread())
                PyErr_Restore(NULL, NULL, NULL);


Does anybody see any problem with this approach ?, Does anybody have a
cleaner/better solution ?
I've allready posted this in comp.lang.python a couple of days ago but
got no answers, that's why I'm posting here, coz I think the only ones
that could give me a solution/workaround are Python core developers.
Thanks a lot.

More information about the Python-Dev mailing list