Acquiring the GUIL is explained clearly in Non-Python created threads subsection of Thread State and the Global Interpreter Lock section in Python manual. But you say the issue is how to do this from SWIG generated code. How about this, added to your SWIG .i file:
struct PythonCallback: public Callback
{
virtual void call() {
getGIL();
lockedCall();
releaseGIL();
}
virtual void lockedCall() {} // will be overwritten in Python
};
You only need to expose PythonCallback
(not Callback) and its lockedCall
method, which you can rename to call
if you wish via SWIG's %rename
directive. Then in Python you would derive from PythonCallback
, and override the call
method (which in reality is the lockedCall
method if you %rename
'd it).