From a7a78879dbe209ea79693bda4640b36bf41276b1 Mon Sep 17 00:00:00 2001 From: prascle Date: Wed, 18 May 2005 09:48:26 +0000 Subject: [PATCH] PR: embedded container C++ & Python --- bin/runSalome.py | 34 +++++++-------- src/Container/Container_i.cxx | 52 ++++++++++++----------- src/Container/Container_init_python.cxx | 56 +++++++++++++++++++++++++ src/Container/Container_init_python.hxx | 44 +++++++++++++++++++ src/Container/Makefile.in | 9 +++- src/Container/SALOME_Container.cxx | 5 +++ src/LifeCycleCORBA/Launchers.cxx | 9 ++++ src/SALOMEGUI/PyInterp_PyQt.cxx | 5 ++- src/SALOMEGUI/PyInterp_base.cxx | 48 ++++++++++----------- src/SALOMEGUI/PyInterp_base.h | 4 +- src/SALOMEGUI/QAD_PyInterp.cxx | 4 +- src/Session/SALOME_Session_Server.cxx | 24 +++++++++++ src/TestContainer/TestContainer.cxx | 44 ++++++++++++------- 13 files changed, 252 insertions(+), 86 deletions(-) create mode 100644 src/Container/Container_init_python.cxx create mode 100644 src/Container/Container_init_python.hxx diff --git a/bin/runSalome.py b/bin/runSalome.py index 055268c56..1c47d008a 100755 --- a/bin/runSalome.py +++ b/bin/runSalome.py @@ -564,23 +564,6 @@ def startSalome(args, modules_list, modules_root_dir): myCmServer.setpath(modules_list,modules_root_dir) myCmServer.run() - # - # Lancement Session Server - # - - mySessionServ = SessionServer(args) - mySessionServ.setpath(modules_list,modules_root_dir) - mySessionServ.run() - - #macomm2=['ddd'] - #pid = os.spawnvp(os.P_NOWAIT, macomm2[0], macomm2) - # - # Attente de la disponibilite du Session Server dans le Naming Service - # - - import SALOME - session=clt.waitNS("/Kernel/Session",SALOME.Session) - from Utils_Identity import getShortHostName if os.getenv("HOSTNAME") == None: @@ -623,6 +606,23 @@ def startSalome(args, modules_list, modules_root_dir): myServer.run() clt.waitNS("/Containers/" + theComputer + "/SuperVisionContainer") + # + # Lancement Session Server + # + + mySessionServ = SessionServer(args) + mySessionServ.setpath(modules_list,modules_root_dir) + mySessionServ.run() + + #macomm2=['ddd'] + #pid = os.spawnvp(os.P_NOWAIT, macomm2[0], macomm2) + # + # Attente de la disponibilite du Session Server dans le Naming Service + # + + import SALOME + session=clt.waitNS("/Kernel/Session",SALOME.Session) + end_time = os.times() print print "Start SALOME, elapsed time : %5.1f seconds"% (end_time[4] diff --git a/src/Container/Container_i.cxx b/src/Container/Container_i.cxx index c2c346cc2..f6374609a 100644 --- a/src/Container/Container_i.cxx +++ b/src/Container/Container_i.cxx @@ -38,6 +38,7 @@ #include #include #include +#include "Container_init_python.hxx" #include "utilities.h" using namespace std; @@ -56,22 +57,23 @@ extern "C" {void ActSigIntHandler() ; } extern "C" {void SigIntHandler(int, siginfo_t *, void *) ; } -static PyMethodDef MethodPyVoidMethod[] = {{ NULL, NULL }}; -PyThreadState *gtstate; +// static PyMethodDef MethodPyVoidMethod[] = {{ NULL, NULL }}; +// PyThreadState *gtstate; -void init_python(int argc, char **argv) -{ - if (gtstate) - return; - Py_SetProgramName(argv[0]); - Py_Initialize(); // Initialize the interpreter - PySys_SetArgv(argc, argv); - PyEval_InitThreads(); // Create (and acquire) the interpreter lock - Py_InitModule( "InitPyRunMethod" , MethodPyVoidMethod ) ; - //PyOS_setsig(SIGSEGV,&Handler); - //PyOS_setsig(SIGINT,&Handler); - gtstate = PyEval_SaveThread(); // Release the global thread state -} +// void init_python(int argc, char **argv) +// { +// if (gtstate) +// return; +// Py_SetProgramName(argv[0]); +// Py_Initialize(); // Initialize the interpreter +// PySys_SetArgv(argc, argv); +// PyEval_InitThreads(); // Create (and acquire) the interpreter lock +// Py_InitModule( "InitPyRunMethod" , MethodPyVoidMethod ) ; +// //PyOS_setsig(SIGSEGV,&Handler); +// //PyOS_setsig(SIGINT,&Handler); +// gtstate = PyEval_SaveThread(); // Release the global thread state +// SCRUTE(gtstate); +// } const char *Engines_Container_i::_defaultContainerName="FactoryServer"; map Engines_Container_i::_cntInstances_map; @@ -164,18 +166,17 @@ Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb, // import SALOME_Container // pycont = SALOME_Container.SALOME_Container_i(containerIORStr) - init_python(argc,argv); - CORBA::String_var sior = _orb->object_to_string(pCont); string myCommand="pyCont = SALOME_Container.SALOME_Container_i('"; myCommand += sior; myCommand += "')\n"; SCRUTE(myCommand); - PyEval_RestoreThread(gtstate); + SCRUTE(KERNEL_PYTHON::_gtstate); + PyEval_RestoreThread(KERNEL_PYTHON::_gtstate); PyRun_SimpleString("import SALOME_Container\n"); PyRun_SimpleString((char*)myCommand.c_str()); - PyEval_ReleaseThread(gtstate); + PyEval_ReleaseThread(KERNEL_PYTHON::_gtstate); } } @@ -301,17 +302,19 @@ Engines_Container_i::load_component_Library(const char* componentName) INFOS("Can't load shared library : " << impl_name); INFOS("error dlopen: " << dlerror()); } - _numInstanceMutex.unlock(); + _numInstanceMutex.unlock(); // --- try import Python component + INFOS("try import Python component "<string_to_object(iors.c_str()); iobject = Engines::Component::_narrow( obj ) ; diff --git a/src/Container/Container_init_python.cxx b/src/Container/Container_init_python.cxx new file mode 100644 index 000000000..1aac1d05c --- /dev/null +++ b/src/Container/Container_init_python.cxx @@ -0,0 +1,56 @@ +// SALOME Container : implementation of container and engine for Kernel +// +// Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org +// +// +// +// File : Container_init_python.cxx +// Author : Paul RASCLE, EDF +// Module : KERNEL +// $Header$ + +#include "Container_init_python.hxx" + +#include "utilities.h" +using namespace std; + +PyThreadState *KERNEL_PYTHON::_gtstate = 0; +PyObject *KERNEL_PYTHON::salome_shared_modules_module = NULL; + +void KERNEL_PYTHON::init_python(int argc, char **argv) +{ + if (Py_IsInitialized()) + { + MESSAGE("Python already initialized"); + SCRUTE(KERNEL_PYTHON::_gtstate); + return; + } + MESSAGE("================================================================="); + MESSAGE("Python Initialization..."); + MESSAGE("================================================================="); + Py_SetProgramName(argv[0]); + Py_Initialize(); // Initialize the interpreter + PySys_SetArgv(argc, argv); + 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); +} + diff --git a/src/Container/Container_init_python.hxx b/src/Container/Container_init_python.hxx new file mode 100644 index 000000000..c28e99cd5 --- /dev/null +++ b/src/Container/Container_init_python.hxx @@ -0,0 +1,44 @@ +// SALOME Container : implementation of container and engine for Kernel +// +// Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org +// +// +// +// File : Container_init_python.hxx +// Author : Paul RASCLE, EDF +// Module : KERNEL +// $Header$ + +#ifndef _CONTAINER_INIT_PYTHON_HXX_ +#define _CONTAINER_INIT_PYTHON_HXX_ + +#include // must be before Python.h ! +#include + +struct KERNEL_PYTHON +{ + static PyThreadState *_gtstate; + static PyObject *salome_shared_modules_module; + + static void init_python(int argc, char **argv); + +}; + +#endif diff --git a/src/Container/Makefile.in b/src/Container/Makefile.in index 7098ace15..5ccebde83 100644 --- a/src/Container/Makefile.in +++ b/src/Container/Makefile.in @@ -41,12 +41,17 @@ EXPORT_PYSCRIPTS = SALOME_ComponentPy.py \ EXPORT_HEADERS = \ SALOME_Component_i.hxx \ SALOME_Container_i.hxx \ - SALOME_ContainerManager.hxx + SALOME_ContainerManager.hxx \ + Container_init_python.hxx # Libraries targets LIB = libSalomeContainer.la -LIB_SRC = Component_i.cxx Container_i.cxx SALOME_ContainerManager.cxx +LIB_SRC = Component_i.cxx \ + Container_i.cxx \ + SALOME_ContainerManager.cxx \ + Container_init_python.cxx + LIB_SERVER_IDL = SALOME_Registry.idl SALOME_Component.idl SALOME_ContainerManager.idl LIB_CLIENT_IDL = diff --git a/src/Container/SALOME_Container.cxx b/src/Container/SALOME_Container.cxx index bd7d8539b..bce0641a2 100644 --- a/src/Container/SALOME_Container.cxx +++ b/src/Container/SALOME_Container.cxx @@ -48,6 +48,7 @@ #endif #include +#include "Container_init_python.hxx" using namespace std; @@ -64,6 +65,10 @@ int main(int argc, char* argv[]) SALOMETraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb); INFOS_COMPILATION; BEGIN_OF(argv[0]); + + int _argc = 1; + char* _argv[] = {""}; + KERNEL_PYTHON::init_python(_argc,_argv); try{ // Obtain a reference to the root POA. diff --git a/src/LifeCycleCORBA/Launchers.cxx b/src/LifeCycleCORBA/Launchers.cxx index f3622465d..3957e8f77 100644 --- a/src/LifeCycleCORBA/Launchers.cxx +++ b/src/LifeCycleCORBA/Launchers.cxx @@ -1,4 +1,7 @@ #include +#include "utilities.h" + +using namespace std; static int Launchers_IsLoaded = 0; static PyObject * Launchers_module = 0; @@ -30,8 +33,14 @@ void releaseMainThread(PyThreadState *tstate){ } void Launchers_assertInitialized() { + MESSAGE("==========================================================="); + MESSAGE("Launchers_assertInitialized"); + MESSAGE("==========================================================="); PyThreadState *_save; if( !Py_IsInitialized() ) { + MESSAGE("==========================================================="); + MESSAGE("Py_Initialize()"); + MESSAGE("==========================================================="); Py_Initialize(); PyEval_InitThreads(); PyEval_SaveThread(); diff --git a/src/SALOMEGUI/PyInterp_PyQt.cxx b/src/SALOMEGUI/PyInterp_PyQt.cxx index 29077ca0e..6c33ce846 100644 --- a/src/SALOMEGUI/PyInterp_PyQt.cxx +++ b/src/SALOMEGUI/PyInterp_PyQt.cxx @@ -10,6 +10,7 @@ // $Header$ #include "PyInterp_PyQt.h" // this include must be first (see PyInterp_base.h)! +#include "Container_init_python.hxx" #include "utilities.h" using namespace std; @@ -35,8 +36,8 @@ void PyInterp_PyQt::initState() * The GIL is acquired in initState and will be held on initState exit * It is the caller responsability to release the lock on exit if needed */ - SCRUTE(PyInterp_base::_gtstate); - _tstate=PyInterp_base::_gtstate; + SCRUTE(KERNEL_PYTHON::_gtstate); + _tstate=KERNEL_PYTHON::_gtstate; PyEval_AcquireLock(); PyThreadState_Swap(_tstate); SCRUTE(_tstate); diff --git a/src/SALOMEGUI/PyInterp_base.cxx b/src/SALOMEGUI/PyInterp_base.cxx index 1c4879d1d..844bc8545 100644 --- a/src/SALOMEGUI/PyInterp_base.cxx +++ b/src/SALOMEGUI/PyInterp_base.cxx @@ -14,6 +14,7 @@ #include #include "PyInterp_base.h" // this include must be first (see PyInterp_base.h)! +#include "Container_init_python.hxx" #include #include @@ -25,8 +26,8 @@ using namespace std; #ifdef _DEBUG_ -static int MYDEBUG = 0; -static int MYPYDEBUG = 0; +static int MYDEBUG = 1; +static int MYPYDEBUG = 1; #else static int MYDEBUG = 0; static int MYPYDEBUG = 0; @@ -38,7 +39,8 @@ static QMutex myMutex(true); PyLockWrapper::PyLockWrapper(PyThreadState* theThreadState): myThreadState(theThreadState), - mySaveThreadState(PyInterp_base::_gtstate) + //mySaveThreadState(PyInterp_base::_gtstate) + mySaveThreadState(KERNEL_PYTHON::_gtstate) { PyEval_AcquireLock(); mySaveThreadState = PyThreadState_Swap(myThreadState); // store previous current in save, @@ -93,12 +95,12 @@ PyLockWrapper PyInterp_base::GetLockWrapper(){ // main python interpreter -PyThreadState *PyInterp_base::_gtstate = 0; // force 0 before execution +// PyThreadState *PyInterp_base::_gtstate = 0; // force 0 before execution int PyInterp_base::_argc = 1; char* PyInterp_base::_argv[] = {""}; PyObject *PyInterp_base::builtinmodule = NULL; -PyObject *PyInterp_base::salome_shared_modules_module = NULL; +//PyObject *PyInterp_base::salome_shared_modules_module = NULL; /*! @@ -129,9 +131,9 @@ void PyInterp_base::initialize() _history.clear(); // start a new list of user's commands _ith = _history.begin(); - if(!_gtstate) init_python(); + init_python(); // Here the global lock is released - if(MYPYDEBUG) MESSAGE("PyInterp_base::initialize() - this = "< #include CORBA_SERVER_HEADER(SALOME_Session) @@ -90,6 +91,8 @@ static int MYDEBUG = 0; static int MYDEBUG = 0; #endif +PyObject *salome_shared_modules_module = NULL; + void MessageOutput( QtMsgType type, const char *msg ) { switch ( type ) { @@ -113,6 +116,27 @@ int main(int argc, char **argv) CORBA::ORB_var &orb = init( orbArgc , argv ) ; SALOMETraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb); qInstallMsgHandler( MessageOutput ); + + /* + * Python initialisation : only once + */ + int _argc = 1; + char* _argv[] = {""}; + KERNEL_PYTHON::init_python(_argc,_argv); + PyEval_RestoreThread(KERNEL_PYTHON::_gtstate); + if(!KERNEL_PYTHON::salome_shared_modules_module) // import only once + { + KERNEL_PYTHON::salome_shared_modules_module = + PyImport_ImportModule("salome_shared_modules"); + } + if(!KERNEL_PYTHON::salome_shared_modules_module) + { + INFOS("salome_shared_modules_module == NULL"); + PyErr_Print(); + PyErr_Clear(); + } + PyEval_ReleaseThread(KERNEL_PYTHON::_gtstate); + try { CORBA::Object_var obj = orb->resolve_initial_references("RootPOA"); diff --git a/src/TestContainer/TestContainer.cxx b/src/TestContainer/TestContainer.cxx index d0e0fe234..89137f358 100644 --- a/src/TestContainer/TestContainer.cxx +++ b/src/TestContainer/TestContainer.cxx @@ -60,13 +60,16 @@ static ostream& operator<<(ostream& os, const CORBA::Exception& e) return os; } -Engines::TestComponent_ptr create_intance(Engines::Container_ptr iGenFact) +Engines::TestComponent_ptr create_instance(Engines::Container_ptr iGenFact, + string componenttName) { bool isLib = - iGenFact->load_component_Library("SalomeTestComponent"); + iGenFact->load_component_Library(componenttName.c_str()); + // iGenFact->load_component_Library("SalomeTestComponent"); ASSERT(isLib); CORBA::Object_var obj = - iGenFact->create_component_instance("SalomeTestComponent", + // iGenFact->create_component_instance("SalomeTestComponent", + iGenFact->create_component_instance(componenttName.c_str(), 0); Engines::TestComponent_var anInstance = Engines::TestComponent::_narrow(obj); MESSAGE("create anInstance"); @@ -99,7 +102,7 @@ int main (int argc, char * argv[]) MESSAGE("------------------------------- create instances "); for (int iter = 0; iter < nbInstances ; iter++) { - instances[iter] = create_intance(iGenFact); + instances[iter] = create_instance(iGenFact,"SalomeTestComponent"); } MESSAGE("------------------------------ set env instances "); @@ -149,16 +152,29 @@ int main (int argc, char * argv[]) } MESSAGE("------------------------------- PYTHON "); { - bool isLib = - iGenFact->load_component_Library("SALOME_TestComponentPy"); - ASSERT(isLib); - CORBA::Object_var obj = - iGenFact->create_component_instance("SALOME_TestComponentPy", - 0); - Engines::TestComponent_var anInstance = - Engines::TestComponent::_narrow(obj); - MESSAGE("create anInstance"); - SCRUTE(anInstance->instanceName()); +// bool isLib = +// iGenFact->load_component_Library("SALOME_TestComponentPy"); +// ASSERT(isLib); +// CORBA::Object_var obj = +// iGenFact->create_component_instance("SALOME_TestComponentPy", +// 0); +// Engines::TestComponent_var anInstance = +// Engines::TestComponent::_narrow(obj); +// MESSAGE("create anInstance"); +// SCRUTE(anInstance->instanceName()); + MESSAGE("------------------------------- create instances "); + for (int iter = 0; iter < nbInstances ; iter++) + { + instances[iter] = create_instance(iGenFact,"SALOME_TestComponentPy"); + } + + MESSAGE("---------------------------------- get instances "); + for (int iter = 0; iter < nbInstances ; iter++) + { + Engines::TestComponent_var anInstance = instances[iter]; + SCRUTE(anInstance->instanceName()); + MESSAGE("Coucou " << anInstance->Coucou(iter)); + } } // Clean-up. -- 2.39.2