2

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?

James
  • 24,676
  • 13
  • 84
  • 130

3 Answers3

4

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
 */
alwinlin
  • 703
  • 1
  • 5
  • 21
1

If you are attempting to call back into Python from a non-Python created thread, then you can use the PyGILState_Ensure() API. This takes care of creating a temporary thread state if the current thread doesn't already have one.

See http://docs.python.org/c-api/init.html#non-python-created-threads for more details.

Releasing the GIL in code that is called from a Python thread is even easier using the Py_BEGIN(/END)_ALLOW_THREADS macros: http://docs.python.org/c-api/init.html#releasing-the-gil-from-extension-code

ncoghlan
  • 40,168
  • 10
  • 71
  • 80
  • The problem is actually the reverse: calling from python into a library, during which time I want to allow other python threads to run. – James Feb 14 '11 at 08:29
  • In that case, you probably want the Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS macros: http://docs.python.org/c-api/init.html#releasing-the-gil-from-extension-code – ncoghlan Feb 15 '11 at 05:09
  • These macros expand to calls to PyEval_SaveThread and PyEval_RestoreThread, which need a PyThreadState to exist for the current thread. It turns out that the fact I don't have a PyThreadState is, in fact, a fatal error. – James Feb 15 '11 at 09:46
1

It turns out that not having a PyThreadState in the main thread is a fatal error. It was caused for me by two different python versions both being linked at the same time, so this question is somewhat moot.

James
  • 24,676
  • 13
  • 84
  • 130