Raising an exception from a multithreaded embedded Python interpreter trace function

Francis Joanis fjoanis at videotron.ca
Fri Jun 6 15:25:23 CEST 2003


Hi everyone,

I managed to embed python into a multithreaded gnu C++ app (class
ThreadedPythonInterpreter). Basically, the app can instantiate python
interpreters, each of them running in a separate thread.

Each python interpreter thread (class PythonThread) has a static trace
function. One of the trace function's parameters is an integer ID
(passed using *obj parameter) that enables the static trace function
to know which embedded python interpreter is calling it.

Inside the trace function I check a boolean variable associated with
the current calling interpreter. If the boolean variable is set to
true, I want the calling interpreter process to stop executing python
code.

What I was thinking about is generating a KeyboardInterrupt exception
inside the trace function, i.e.:

int PythonThread::traceFunc(PyObject *obj, PyFrameObject *frame, int
what, PyObject *arg) {
	if(!((ThreadedPythonInterpreter*)pParent)->getThread(PyInt_AsLong(obj))->threadCanRun())
	
	PyErr_Format(PyExc_KeyboardInterrupt,"");
	
	return 0;
}

The problem is that the PyErr_Format(...) call just does not seem to
do anything. I don't have any try/except blocks into the running
scripts and I believe my PyThreadState_Swap and PyEval_AcquireLock
(...) calling schemes are OK since I am able to run scripts in
separate threads.

Any hints?

Thank you,

Francis Joanis




More information about the Python-list mailing list