error handling in multithreaded extension callbacks

C

Connor

Hi,

In my extension I'm calling python functions as callbacks from a thread
generated in an external module.
This works very well, but I'm not sure about the error handling.

1. Normally the python interpreter exits if it runs on an unhandled
error. Is this the preferred standard behavior for this case too ?
2. How can I implement this in my error handling ?

Here is my code so far. If an error happens in the python code it is
printed to stderr, but the python script is still running in the main
thread.

void FUNC_C_DECL coreCommandReadyCallback(TML_COMMAND_HANDLE cmd,
TML_POINTER data)
{
// Calling Python out of a non- python created thread
PyGILState_STATE gstate;
try {
gstate = PyGILState_Ensure();

PythonCallbackData* callbackData = (PythonCallbackData*) data;

PyObject *arglist;

PyObject *pythonCBFunc = callbackData->pCBFunc;
/* Time to call the callback */
arglist = Py_BuildValue("(LO)", cmd, callbackData->pCBData);
PyObject* result = PyEval_CallObject(pythonCBFunc, arglist);
Py_DECREF(arglist);

if ( PyErr_Occurred() ) {
PyErr_Print();
PyErr_SetString(PyExc_TypeError, "PyErr_Occurred");
}


// Release calling Python out of a non- python created thread
PyGILState_Release(gstate);
}
catch (...) {
printf ("An Exception Happened\n");
}
}

Cheers,
Connor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top