How do I get the current PyInterpreterState?

advertisements

I'd like to create a PyThreadState since there doesn't appear to be one for the current thread.

How do I get the current PyInterpreterState to pass to PyThreadState_New(...), or is that something that I should only do if I'm embedding an interpreter (and thus have created it), rather than calling into a library from python?


I guess that you might are doing multi-threads programming. Recently, I'm having the similar problems. Hope the following codes may help you.

//in main thread
PyThreadState * mainThreadState = NULL;

Py_Initialize();
PyEval_InitThreads();
mainThreadState = PyThreadState_Get();
PyEval_ReleaseLock();

//in threaded thread
PyEval_AcquireLock();
PyInterpreterState * mainInterpreterState = mainThreadState->interp;
PyThreadState * myThreadState = PyThreadState_New(mainInterpreterState);
PyEval_ReleaseLock();
/*
 * embeded python part
 * PyEval_CallObject() for example
 */