PyThreadState_SetAsyncExc, PyErr_Clear and boost::python

gabriel.becedillas at gmail.com gabriel.becedillas at gmail.com
Wed May 10 14:23:08 EDT 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
acceptable).
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 functions).
Example:

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 ?
Thanks.




More information about the Python-list mailing list