]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
[EDF27816] : Usage of AutoGIL RAII to protect python calls
authorAnthony Geay <anthony.geay@edf.fr>
Fri, 18 Aug 2023 13:36:04 +0000 (15:36 +0200)
committerAnthony Geay <anthony.geay@edf.fr>
Fri, 18 Aug 2023 13:36:04 +0000 (15:36 +0200)
src/Container/Container_i.cxx
src/Container/Container_init_python.hxx
src/MPIContainer/MPIContainer_i.cxx
src/ParallelContainer/SALOME_ParallelContainerProxy_i.cxx
src/ParallelContainer/SALOME_ParallelContainer_i.cxx

index efc594a8ced172f4ce953714b0de937f2b13f6d1..b502a919268b8d0a9b8c01735e28b957ad94c6d4 100644 (file)
@@ -59,6 +59,7 @@ int SIGUSR1 = 1000;
 #include "SALOME_Embedded_NamingService_Client.hxx"
 #include "SALOME_Embedded_NamingService.hxx"
 #include "Basics_Utils.hxx"
+#include "PythonCppUtils.hxx"
 
 #ifdef _XOPEN_SOURCE
 #undef _XOPEN_SOURCE
@@ -215,34 +216,33 @@ Abstract_Engines_Container_i::Abstract_Engines_Container_i (CORBA::ORB_ptr orb,
     //approach leads to the deadlock of the main thread of the application on Windows platform
     //in case if cppContainer runs in the standalone mode. The problem with the PyThreadState 
     //described by ABN seems not reproduced, to be checked carefully later...
-    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:    
-    //PyEval_AcquireLock();  // get GIL
-    //PyThreadState * mainThreadState = PyThreadState_Get();
-    //PyThreadState_Swap(mainThreadState);
+    {
+      AutoGIL gstate;    
+      //// [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:    
+      //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
-    //Sleep(2000);
-    //
-    // first element is the path to Registry.dll, but it's wrong
-    PyRun_SimpleString("import sys\n");
-    PyRun_SimpleString("sys.path = sys.path[1:]\n");
+      // mpv: this is temporary solution: there is a unregular crash if not
+      //Sleep(2000);
+      //
+      // first element is the path to Registry.dll, but it's wrong
+      PyRun_SimpleString("import sys\n");
+      PyRun_SimpleString("sys.path = sys.path[1:]\n");
 #endif
-    PyRun_SimpleString("import SALOME_Container\n");
-    PyRun_SimpleString((char*)myCommand.c_str());
-    PyObject *mainmod = PyImport_AddModule("__main__");
-    PyObject *globals = PyModule_GetDict(mainmod);
-    _pyCont = PyDict_GetItemString(globals, "pyCont");
-
-    //PyThreadState_Swap(NULL);
-    //PyEval_ReleaseLock();
-    PyGILState_Release(gstate);
+      PyRun_SimpleString("import SALOME_Container\n");
+      PyRun_SimpleString((char*)myCommand.c_str());
+      PyObject *mainmod = PyImport_AddModule("__main__");
+      PyObject *globals = PyModule_GetDict(mainmod);
+      _pyCont = PyDict_GetItemString(globals, "pyCont");
+      //PyThreadState_Swap(NULL);
+      //PyEval_ReleaseLock();
+    }
 
     fileTransfer_i* aFileTransfer = new fileTransfer_i();
     CORBA::Object_var obref=aFileTransfer->_this();
@@ -357,13 +357,12 @@ void Abstract_Engines_Container_i::ping()
 
 CORBA::Long Abstract_Engines_Container_i::getNumberOfCPUCores()
 {
-  PyGILState_STATE gstate = PyGILState_Ensure();
+  AutoGIL gstate;
   PyObject *module = PyImport_ImportModuleNoBlock((char*)"salome_psutil");
   PyObject *result = PyObject_CallMethod(module,
                                          (char*)"getNumberOfCPUCores", NULL);
   int n = PyLong_AsLong(result);
   Py_DECREF(result);
-  PyGILState_Release(gstate);
 
   return (CORBA::Long)n;
 }
@@ -520,7 +519,7 @@ namespace {
   
 Engines::vectorOfDouble* Abstract_Engines_Container_i::loadOfCPUCores()
 {
-  PyGILState_STATE gstate = PyGILState_Ensure();
+  AutoGIL gstate;
   PyObject *module = PyImport_ImportModuleNoBlock((char*)"salome_psutil");
   PyObject *result = PyObject_CallMethod(module,
                                          (char*)"loadOfCPUCores", "s",
@@ -529,7 +528,6 @@ Engines::vectorOfDouble* Abstract_Engines_Container_i::loadOfCPUCores()
   {
     std::string error = parseException();
     PyErr_Print();
-    PyGILState_Release(gstate);
     SALOME::ExceptionStruct es;
     es.type = SALOME::INTERNAL_ERROR;
     es.text = CORBA::string_dup(error.c_str());
@@ -539,7 +537,6 @@ Engines::vectorOfDouble* Abstract_Engines_Container_i::loadOfCPUCores()
   int n = this->getNumberOfCPUCores();
   if (!PyList_Check(result) || PyList_Size(result) != n) {
     // bad number of cores
-    PyGILState_Release(gstate);
     Py_DECREF(result);
     SALOME::ExceptionStruct es;
     es.type = SALOME::INTERNAL_ERROR;
@@ -555,7 +552,6 @@ Engines::vectorOfDouble* Abstract_Engines_Container_i::loadOfCPUCores()
     if (foo < 0.0 || foo > 1.0)
     {
       // value not in [0, 1] range
-      PyGILState_Release(gstate);
       Py_DECREF(result);
       SALOME::ExceptionStruct es;
       es.type = SALOME::INTERNAL_ERROR;
@@ -566,7 +562,6 @@ Engines::vectorOfDouble* Abstract_Engines_Container_i::loadOfCPUCores()
   }
 
   Py_DECREF(result);
-  PyGILState_Release(gstate);
 
   return loads._retn();
 }
@@ -605,13 +600,12 @@ void Abstract_Engines_Container_i::resetScriptForCPULoad()
 
 CORBA::Long Abstract_Engines_Container_i::getTotalPhysicalMemory()
 {
-  PyGILState_STATE gstate = PyGILState_Ensure();
+  AutoGIL gstate;
   PyObject *module = PyImport_ImportModuleNoBlock((char*)"salome_psutil");
   PyObject *result = PyObject_CallMethod(module,
                                          (char*)"getTotalPhysicalMemory", NULL);
   int n = PyLong_AsLong(result);
   Py_DECREF(result);
-  PyGILState_Release(gstate);
 
   return (CORBA::Long)n;
 }
@@ -625,13 +619,12 @@ CORBA::Long Abstract_Engines_Container_i::getTotalPhysicalMemory()
 
 CORBA::Long Abstract_Engines_Container_i::getTotalPhysicalMemoryInUse()
 {
-  PyGILState_STATE gstate = PyGILState_Ensure();
+  AutoGIL gstate;
   PyObject *module = PyImport_ImportModuleNoBlock((char*)"salome_psutil");
   PyObject *result = PyObject_CallMethod(module,
                                          (char*)"getTotalPhysicalMemoryInUse", NULL);
   int n = PyLong_AsLong(result);
   Py_DECREF(result);
-  PyGILState_Release(gstate);
 
   return (CORBA::Long)n;
 }
@@ -645,13 +638,12 @@ CORBA::Long Abstract_Engines_Container_i::getTotalPhysicalMemoryInUse()
 
 CORBA::Long Abstract_Engines_Container_i::getTotalPhysicalMemoryInUseByMe()
 {
-  PyGILState_STATE gstate = PyGILState_Ensure();
+  AutoGIL gstate;
   PyObject *module = PyImport_ImportModuleNoBlock((char*)"salome_psutil");
   PyObject *result = PyObject_CallMethod(module,
                                          (char*)"getTotalPhysicalMemoryInUseByMe", NULL);
   int n = PyLong_AsLong(result);
   Py_DECREF(result);
-  PyGILState_Release(gstate);
 
   return (CORBA::Long)n;
 }
@@ -915,15 +907,16 @@ Abstract_Engines_Container_i::load_component_PythonImplementation(const char* co
   }
   _numInstanceMutex.unlock() ;
 
-  PyGILState_STATE gstate = PyGILState_Ensure();
-  PyObject *result = PyObject_CallMethod(_pyCont,
-                                         (char*)"import_component",
-                                         (char*)"s",componentName);
+  {
+    AutoGIL gstate;
+    PyObject *result = PyObject_CallMethod(_pyCont,
+                                          (char*)"import_component",
+                                          (char*)"s",componentName);
 
-  reason=PyUnicode_AsUTF8(result);
-  Py_XDECREF(result);
-  SCRUTE(reason);
-  PyGILState_Release(gstate);
+    reason=PyUnicode_AsUTF8(result);
+    Py_XDECREF(result);
+    SCRUTE(reason);
+  }
 
   if (reason=="")
   {
@@ -1251,20 +1244,21 @@ Abstract_Engines_Container_i::createPythonInstance(std::string CompName,
   sprintf( aNumI , "%d" , numInstance ) ;
   std::string instanceName = CompName + "_inst_" + aNumI ;
   std::string component_registerName = _containerName + "/" + instanceName;
-
-  PyGILState_STATE gstate = PyGILState_Ensure();
-  PyObject *result = PyObject_CallMethod(_pyCont,
-                                         (char*)"create_component_instance",
-                                         (char*)"ss",
-                                         CompName.c_str(),
-                                         instanceName.c_str());
-  const char *ior;
-  const char *error;
-  PyArg_ParseTuple(result,"ss", &ior, &error);
-  std::string iors = ior;
-  reason=error;
-  Py_DECREF(result);
-  PyGILState_Release(gstate);
+  std::string iors;
+  {
+    AutoGIL gstate;
+    PyObject *result = PyObject_CallMethod(_pyCont,
+                                          (char*)"create_component_instance",
+                                          (char*)"ss",
+                                          CompName.c_str(),
+                                          instanceName.c_str());
+    const char *ior;
+    const char *error;
+    PyArg_ParseTuple(result,"ss", &ior, &error);
+    iors = ior;
+    reason=error;
+    Py_DECREF(result);
+  }
 
   if( iors!="" )
   {
@@ -1291,20 +1285,21 @@ Abstract_Engines_Container_i::create_python_service_instance(const char * CompNa
   std::string instanceName = std::string(CompName) + "_inst_" + aNumI ;
   std::string component_registerName = _containerName + "/" + instanceName;
 
-  PyGILState_STATE gstate = PyGILState_Ensure();
-  PyObject *result = PyObject_CallMethod(_pyCont,
-                                         (char*)"create_component_instance",
-                                         (char*)"ss",
-                                         CompName,
-                                         instanceName.c_str());
-  const char *ior;
-  const char *error;
-  PyArg_ParseTuple(result,"ss", &ior, &error);
-  reason = CORBA::string_dup(error);
-  char * _ior = CORBA::string_dup(ior);
-  Py_DECREF(result);
-  PyGILState_Release(gstate);
-
+  char * _ior = nullptr;
+  {
+    AutoGIL gstate;
+    PyObject *result = PyObject_CallMethod(_pyCont,
+                                          (char*)"create_component_instance",
+                                          (char*)"ss",
+                                          CompName,
+                                          instanceName.c_str());
+    const char *ior;
+    const char *error;
+    PyArg_ParseTuple(result,"ss", &ior, &error);
+    reason = CORBA::string_dup(error);
+    _ior = CORBA::string_dup(ior);
+    Py_DECREF(result);
+  }
   return _ior;
 }
 
@@ -1961,28 +1956,29 @@ void Abstract_Engines_Container_i::copyFile(Engines::Container_ptr container, co
 Engines::PyNode_ptr Abstract_Engines_Container_i::createPyNode(const char* nodeName, const char* code)
 {
   Engines::PyNode_var node= Engines::PyNode::_nil();
-
-  PyGILState_STATE gstate = PyGILState_Ensure();
-  PyObject *res = PyObject_CallMethod(_pyCont,
-    (char*)"create_pynode",
-    (char*)"ss",
-    nodeName,
-    code);
-  if(res==NULL)
-  {
-    //internal error
-    PyErr_Print();
-    PyGILState_Release(gstate);
-    SALOME::ExceptionStruct es;
-    es.type = SALOME::INTERNAL_ERROR;
-    es.text = "can not create a python node";
-    throw SALOME::SALOME_Exception(es);
+  long ierr(-1);
+  std::string astr;
+  {
+    AutoGIL gstate;
+    PyObject *res = PyObject_CallMethod(_pyCont,
+      (char*)"create_pynode",
+      (char*)"ss",
+      nodeName,
+      code);
+    if(res==NULL)
+    {
+      //internal error
+      PyErr_Print();
+      SALOME::ExceptionStruct es;
+      es.type = SALOME::INTERNAL_ERROR;
+      es.text = "can not create a python node";
+      throw SALOME::SALOME_Exception(es);
+    }
+    ierr=PyLong_AsLong(PyTuple_GetItem(res,0));
+    PyObject* result=PyTuple_GetItem(res,1);
+    astr = PyUnicode_AsUTF8(result);
+    Py_DECREF(res);
   }
-  long ierr=PyLong_AsLong(PyTuple_GetItem(res,0));
-  PyObject* result=PyTuple_GetItem(res,1);
-  std::string astr=PyUnicode_AsUTF8(result);
-  Py_DECREF(res);
-  PyGILState_Release(gstate);
   if(ierr==0)
   {
     Utils_Locker lck(&_mutexForDftPy);
@@ -2044,28 +2040,29 @@ Engines::PyNode_ptr Abstract_Engines_Container_i::getDefaultPyNode(const char *n
 Engines::PyScriptNode_ptr Abstract_Engines_Container_i::createPyScriptNode(const char* nodeName, const char* code)
 {
   Engines::PyScriptNode_var node= Engines::PyScriptNode::_nil();
-
-  PyGILState_STATE gstate = PyGILState_Ensure();
-  PyObject *res = PyObject_CallMethod(_pyCont,
-    (char*)"create_pyscriptnode",
-    (char*)"ss",
-    nodeName,
-    code);
-  if(res==NULL)
-  {
-    //internal error
-    PyErr_Print();
-    PyGILState_Release(gstate);
-    SALOME::ExceptionStruct es;
-    es.type = SALOME::INTERNAL_ERROR;
-    es.text = "can not create a python node";
-    throw SALOME::SALOME_Exception(es);
+  long ierr(-1);
+  std::string astr;
+  {
+    AutoGIL gstate;
+    PyObject *res = PyObject_CallMethod(_pyCont,
+      (char*)"create_pyscriptnode",
+      (char*)"ss",
+      nodeName,
+      code);
+    if(res==NULL)
+    {
+      //internal error
+      PyErr_Print();
+      SALOME::ExceptionStruct es;
+      es.type = SALOME::INTERNAL_ERROR;
+      es.text = "can not create a python node";
+      throw SALOME::SALOME_Exception(es);
+    }
+    ierr=PyLong_AsLong(PyTuple_GetItem(res,0));
+    PyObject* result=PyTuple_GetItem(res,1);
+    astr = PyUnicode_AsUTF8(result);
+    Py_DECREF(res);
   }
-  long ierr=PyLong_AsLong(PyTuple_GetItem(res,0));
-  PyObject* result=PyTuple_GetItem(res,1);
-  std::string astr=PyUnicode_AsUTF8(result);
-  Py_DECREF(res);
-  PyGILState_Release(gstate);
 
   if(ierr==0)
   {
index 105c079df4c763bbc83993ccff1b71da19ff0635..39c7c92e3b0c2aee188fef287fd90556ae2b63e8 100644 (file)
 #endif
 #include <Python.h>
 
-
-// next two MACRO must be used together only once inside a block
-// -------------------------------------------------------------
-// protect a sequence of Python calls:
-// - Python lock must be acquired for these calls
-// - new Python thread state allows multi thread use of the sequence:
-//    - Python may release the lock within the sequence, so multiple
-//      thread execution of the sequence may occur.
-//    - For that case, each sequence call must use a specific Python
-//      thread state.
-//    - There is no need of C Lock protection of the sequence.
-
-
-#define Py_ACQUIRE_NEW_THREAD \
-  PyGILState_STATE gil_state = PyGILState_Ensure();
-
-#define Py_RELEASE_NEW_THREAD \
-  PyGILState_Release(gil_state);
-
 typedef void ContainerPyOutChanged(void* data,char * c);
 
 typedef struct {
index 51f81e577d02fe352729b9bc9fc7a817bf501e9e..00be6f83fb2e718c78fea09e38d086905fe48dd3 100644 (file)
@@ -193,16 +193,17 @@ bool Engines_MPIContainer_i::Lload_component_Library(const char* componentName)
     }
   else
     {
-      Py_ACQUIRE_NEW_THREAD;
-      PyObject *mainmod = PyImport_AddModule((char *)"__main__");
-      PyObject *globals = PyModule_GetDict(mainmod);
-      PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
-      PyObject *result = PyObject_CallMethod(pyCont,
-                                             (char*)"import_component",
-                                             (char*)"s",componentName);
-      std::string ret= PyUnicode_AsUTF8(result);
-      SCRUTE(ret);
-      Py_RELEASE_NEW_THREAD;
+      {
+        AutoGIL agil;
+        PyObject *mainmod = PyImport_AddModule((char *)"__main__");
+        PyObject *globals = PyModule_GetDict(mainmod);
+        PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
+        PyObject *result = PyObject_CallMethod(pyCont,
+                                              (char*)"import_component",
+                                              (char*)"s",componentName);
+        std::string ret= PyUnicode_AsUTF8(result);
+        SCRUTE(ret);
+      }
   
       if (ret=="") // import possible: Python component
         {
@@ -268,21 +269,22 @@ Engines_MPIContainer_i::Lcreate_component_instance( const char* genericRegisterN
     std::string component_registerName =
       _containerName + "/" + instanceName;
 
-    Py_ACQUIRE_NEW_THREAD;
-    PyObject *mainmod = PyImport_AddModule((char*)"__main__");
-    PyObject *globals = PyModule_GetDict(mainmod);
-    PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
-    PyObject *result = PyObject_CallMethod(pyCont,
-                                           (char*)"create_component_instance",
-                                           (char*)"ss",
-                                           aCompName.c_str(),
-                                           instanceName.c_str());
-    const char *ior;
-    const char *error;
-    PyArg_ParseTuple(result,"ss", &ior, &error);
-    std::string iors = ior;
-    SCRUTE(iors);
-    Py_RELEASE_NEW_THREAD;
+    {
+      AutoGIL agil;
+      PyObject *mainmod = PyImport_AddModule((char*)"__main__");
+      PyObject *globals = PyModule_GetDict(mainmod);
+      PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
+      PyObject *result = PyObject_CallMethod(pyCont,
+                                            (char*)"create_component_instance",
+                                            (char*)"ss",
+                                            aCompName.c_str(),
+                                            instanceName.c_str());
+      const char *ior;
+      const char *error;
+      PyArg_ParseTuple(result,"ss", &ior, &error);
+      std::string iors = ior;
+      SCRUTE(iors);
+    }
   
     CORBA::Object_var obj = _orb->string_to_object(iors.c_str());
     iobject = Engines::EngineComponent::_narrow( obj ) ;
index 786e785722cf919adc83933096047580f2d1be8b..285318d9ec311f0b63a573a153513bf9bc51dd66 100644 (file)
@@ -57,10 +57,11 @@ Container_proxy_impl_final::Container_proxy_impl_final(CORBA::ORB_ptr orb,
   myCommand += _containerName + "','";
   myCommand += sior;
   myCommand += "')\n";
-  Py_ACQUIRE_NEW_THREAD;
-  PyRun_SimpleString("import SALOME_Container\n");
-  PyRun_SimpleString((char*)myCommand.c_str());
-  Py_RELEASE_NEW_THREAD;
+  {
+    AutoGIL agil;
+    PyRun_SimpleString("import SALOME_Container\n");
+    PyRun_SimpleString((char*)myCommand.c_str());
+  }
 }
 
 Container_proxy_impl_final:: ~Container_proxy_impl_final() {
@@ -209,16 +210,17 @@ Container_proxy_impl_final::load_component_Library(const char* componentName, CO
 #endif
 
       MESSAGE("Try to import Python component "<<componentName);
-      Py_ACQUIRE_NEW_THREAD;
-      PyObject *mainmod = PyImport_AddModule("__main__");
-      PyObject *globals = PyModule_GetDict(mainmod);
-      PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
-      PyObject *result = PyObject_CallMethod(pyCont,
-                                             (char*)"import_component",
-                                             (char*)"s",componentName);
-      std::string ret_p= PyUnicode_AsUTF8(result);
-      Py_XDECREF(result);
-      Py_RELEASE_NEW_THREAD;
+      {
+        AutoGIL agil;
+        PyObject *mainmod = PyImport_AddModule("__main__");
+        PyObject *globals = PyModule_GetDict(mainmod);
+        PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
+        PyObject *result = PyObject_CallMethod(pyCont,
+                                              (char*)"import_component",
+                                              (char*)"s",componentName);
+        std::string ret_p= PyUnicode_AsUTF8(result);
+        Py_XDECREF(result);
+      }
 
       if (ret_p=="") // import possible: Python component
       {
index bffa825454d335b7cd58296940b596e7605b936c..7125c6dc596d1322c71d33e75a960fae90ff9bb4 100644 (file)
@@ -110,10 +110,11 @@ Engines_Parallel_Container_i::Engines_Parallel_Container_i (CORBA::ORB_ptr orb,
   myCommand += _containerName + "','";
   myCommand += sior;
   myCommand += "')\n";
-  Py_ACQUIRE_NEW_THREAD;
-  PyRun_SimpleString("import SALOME_Container\n");
-  PyRun_SimpleString((char*)myCommand.c_str());
-  Py_RELEASE_NEW_THREAD;
+  {
+    AutoGIL agil;
+    PyRun_SimpleString("import SALOME_Container\n");
+    PyRun_SimpleString((char*)myCommand.c_str());
+  }
 
   // Init FileTransfer service
   fileTransfer_i* aFileTransfer = new fileTransfer_i();
@@ -346,16 +347,17 @@ Engines_Parallel_Container_i::load_component_Library(const char* componentName,
   if (!ret)
   {
     MESSAGE("Try to import Python component "<<componentName);
-    Py_ACQUIRE_NEW_THREAD;
-    PyObject *mainmod = PyImport_AddModule("__main__");
-    PyObject *globals = PyModule_GetDict(mainmod);
-    PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
-    PyObject *result = PyObject_CallMethod(pyCont,
-                                           (char*)"import_component",
-                                           (char*)"s",componentName);
-    std::string ret_p= PyUnicode_AsUTF8(result);
-    Py_XDECREF(result);
-    Py_RELEASE_NEW_THREAD;
+    {
+      AutoGIL agil;
+      PyObject *mainmod = PyImport_AddModule("__main__");
+      PyObject *globals = PyModule_GetDict(mainmod);
+      PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
+      PyObject *result = PyObject_CallMethod(pyCont,
+                                            (char*)"import_component",
+                                            (char*)"s",componentName);
+      std::string ret_p= PyUnicode_AsUTF8(result);
+      Py_XDECREF(result);
+    }
 
     if (ret_p=="") // import possible: Python component
     {
@@ -764,21 +766,22 @@ Engines_Parallel_Container_i::createPythonInstance(std::string genericRegisterNa
   std::string instanceName = genericRegisterName + "_inst_" + aNumI ;
   std::string component_registerName = _containerName + "/" + instanceName;
 
-  Py_ACQUIRE_NEW_THREAD;
-  PyObject *mainmod = PyImport_AddModule("__main__");
-  PyObject *globals = PyModule_GetDict(mainmod);
-  PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
-  PyObject *result = PyObject_CallMethod(pyCont,
-                                         (char*)"create_component_instance",
-                                         (char*)"ss",
-                                         genericRegisterName.c_str(),
-                                         instanceName.c_str());
-  const char *ior;
-  const char *error;
-  PyArg_ParseTuple(result,"ss", &ior, &error);
-  string iors = ior;
-  Py_DECREF(result);
-  Py_RELEASE_NEW_THREAD;
+  {
+    AutoGIL agil;
+    PyObject *mainmod = PyImport_AddModule("__main__");
+    PyObject *globals = PyModule_GetDict(mainmod);
+    PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
+    PyObject *result = PyObject_CallMethod(pyCont,
+                                          (char*)"create_component_instance",
+                                          (char*)"ss",
+                                          genericRegisterName.c_str(),
+                                          instanceName.c_str());
+    const char *ior;
+    const char *error;
+    PyArg_ParseTuple(result,"ss", &ior, &error);
+    string iors = ior;
+    Py_DECREF(result);
+  }
 
   if( iors!="" )
   {