PyGILState API and Py_Main

Adrien Bruneton adrien.bruneton at cea.fr
Thu Dec 18 03:08:34 EST 2014


Hello all,

I am having a hard time understanding what is the proper use of 
PyGILState_Ensure/Release.
My understanding is that one should always be matched with the other, 
and that this high level API auto-magically deals with the ThreadState 
creation.

However the following piece of code (executed with a simple "print 
'hello world' " script as argv) triggers the message:

     Fatal Python error: auto-releasing thread-state, but no 
thread-state for this thread

Minimal code:

void initPython(int initsigs)
{
   if (Py_IsInitialized() == 0)
     {
       Py_InitializeEx(initsigs);
       // Put default SIGINT handler back after 
Py_Initialize/Py_InitializeEx.
       signal(SIGINT, SIG_DFL);
     }

   int threadInit = PyEval_ThreadsInitialized();
   PyEval_InitThreads(); // safe to call this multiple time

   if(!threadInit)
     PyEval_SaveThread(); // release GIL
}

int main(int argc, char ** argv)
{
   initPython(1);
   PyGILState_STATE _gstate_avoid_clash = PyGILState_Ensure();
   int ret = Py_Main(argc, argv);
   PyGILState_Release(_gstate_avoid_clash);  // this one triggers the 
Fatal error
   Py_Finalize();
   return ret;
}


Removing the last PyGILState_Release works, but I have a bad feeling 
about it :-)
Any help would be welcome! Thanks in advance.
Adrien.




More information about the Python-list mailing list