From: Adrien Bruneton Date: Wed, 12 Feb 2014 13:44:53 +0000 (+0100) Subject: New Python logic: simplified KERNEL's Python initialization. X-Git-Tag: V7_3_1b1^2~1 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=282e6057636205121c13772e9da6cf768eb5e138;p=modules%2Fkernel.git New Python logic: simplified KERNEL's Python initialization. Saving various thread states and global interpreter state is no more needed when using only one global interpreter. --- diff --git a/src/Container/Container_i.cxx b/src/Container/Container_i.cxx index c2fb5febf..8d9a382eb 100644 --- a/src/Container/Container_i.cxx +++ b/src/Container/Container_i.cxx @@ -201,7 +201,15 @@ Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb, myCommand += "')\n"; SCRUTE(myCommand); - PyGILState_STATE gstate = PyGILState_Ensure(); + // [ABN]: using the PyGILState* API here is unstable. omniORB logic is invoked + // by the Python code executed below, and in some (random) cases, the Python code + // execution ends with a PyThreadState which was not the one we have here. + // (TODO: understand why ...) + // To be on the safe side we get and load the thread state ourselves: + //PyGILState_STATE gstate = PyGILState_Ensure(); + PyEval_AcquireLock(); // get GIL + PyThreadState * mainThreadState = PyThreadState_Get(); + PyThreadState_Swap(mainThreadState); #ifdef WIN32 // mpv: this is temporary solution: there is a unregular crash if not @@ -217,7 +225,9 @@ Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb, PyObject *globals = PyModule_GetDict(mainmod); _pyCont = PyDict_GetItemString(globals, "pyCont"); - PyGILState_Release(gstate); + PyThreadState_Swap(NULL); + PyEval_ReleaseLock(); + //PyGILState_Release(gstate); fileTransfer_i* aFileTransfer = new fileTransfer_i(); CORBA::Object_var obref=aFileTransfer->_this(); diff --git a/src/Container/Container_init_python.cxx b/src/Container/Container_init_python.cxx index 1d8168ee2..8c448bebc 100644 --- a/src/Container/Container_init_python.cxx +++ b/src/Container/Container_init_python.cxx @@ -35,16 +35,11 @@ #include "Container_init_python.hxx" -PyThreadState *KERNEL_PYTHON::_gtstate = 0; -PyObject *KERNEL_PYTHON::salome_shared_modules_module = NULL; -PyInterpreterState *KERNEL_PYTHON::_interp = NULL; - void KERNEL_PYTHON::init_python(int argc, char **argv) { if (Py_IsInitialized()) { MESSAGE("Python already initialized"); - SCRUTE(KERNEL_PYTHON::_gtstate); return; } MESSAGE("================================================================="); @@ -57,10 +52,7 @@ void KERNEL_PYTHON::init_python(int argc, char **argv) Py_SetProgramName(salome_python); Py_Initialize(); // Initialize the interpreter PySys_SetArgv(argc, argv); - KERNEL_PYTHON::_interp = PyThreadState_Get()->interp; PyEval_InitThreads(); // Create (and acquire) the interpreter lock - ASSERT(!KERNEL_PYTHON::_gtstate); - KERNEL_PYTHON::_gtstate = PyEval_SaveThread(); // Release global thread state - SCRUTE(KERNEL_PYTHON::_gtstate); + PyEval_ReleaseLock(); // Py_InitThreads acquires the GIL } diff --git a/src/Container/Container_init_python.hxx b/src/Container/Container_init_python.hxx index 5bb76c5c9..4cda886d2 100644 --- a/src/Container/Container_init_python.hxx +++ b/src/Container/Container_init_python.hxx @@ -53,27 +53,14 @@ #define Py_ACQUIRE_NEW_THREAD \ - PyEval_AcquireLock(); \ - PyThreadState *myTstate = PyThreadState_New(KERNEL_PYTHON::_interp); \ - PyThreadState_Swap(myTstate); + PyGILState_STATE gil_state = PyGILState_Ensure(); #define Py_RELEASE_NEW_THREAD \ - PyEval_ReleaseThread(myTstate); \ - PyThreadState_Delete(myTstate); + PyGILState_Release(gil_state); struct CONTAINER_EXPORT KERNEL_PYTHON { -#ifdef WIN32 - static PyThreadState *get_gtstate() { return KERNEL_PYTHON::_gtstate; } - static PyObject *getsalome_shared_modules_module() { return KERNEL_PYTHON::salome_shared_modules_module; } - static PyInterpreterState *get_interp() { return KERNEL_PYTHON::_interp; } -#endif - static PyThreadState *_gtstate; - static PyObject *salome_shared_modules_module; - static PyInterpreterState *_interp; - static void init_python(int argc, char **argv); - }; #endif