diff --git a/mindspore/_check_version.py b/mindspore/_check_version.py index 865af779ea..0e0ca358c8 100644 --- a/mindspore/_check_version.py +++ b/mindspore/_check_version.py @@ -216,7 +216,8 @@ class AscendEnvChecker(EnvChecker): try: # pylint: disable=unused-import import te - except RuntimeError: + # pylint: disable=broad-except + except Exception: if Path(self.tbe_path).is_dir(): if os.getenv('LD_LIBRARY_PATH'): os.environ['LD_LIBRARY_PATH'] = self.tbe_path + ":" + os.environ['LD_LIBRARY_PATH'] diff --git a/mindspore/ccsrc/cxx_api/graph/ms/ms_graph_impl.cc b/mindspore/ccsrc/cxx_api/graph/ms/ms_graph_impl.cc index 60338f23aa..2839964f17 100644 --- a/mindspore/ccsrc/cxx_api/graph/ms/ms_graph_impl.cc +++ b/mindspore/ccsrc/cxx_api/graph/ms/ms_graph_impl.cc @@ -123,12 +123,13 @@ Status MsGraphImpl::FinalizeEnv() { return FAILED; } - InitPython(); // CloseTsd will release python git - if (!context::CloseTsd(ms_context)) { - MS_LOG(ERROR) << "CloseTsd failed!"; - return FAILED; + { + PythonEnvGuard guard; + if (!context::CloseTsd(ms_context)) { + MS_LOG(ERROR) << "CloseTsd failed!"; + return FAILED; + } } - FinalizePython(); init_flag_ = false; MS_LOG(INFO) << "End finalize env"; diff --git a/mindspore/ccsrc/cxx_api/python_utils.cc b/mindspore/ccsrc/cxx_api/python_utils.cc index 86af6599ba..ecf737dcf5 100644 --- a/mindspore/ccsrc/cxx_api/python_utils.cc +++ b/mindspore/ccsrc/cxx_api/python_utils.cc @@ -131,4 +131,16 @@ void FinalizePython() { Py_Finalize(); } } + +PythonEnvGuard::PythonEnvGuard() { + origin_init_status_ = PythonIsInited(); + InitPython(); +} + +PythonEnvGuard::~PythonEnvGuard() { + // finalize when init by this + if (!origin_init_status_) { + FinalizePython(); + } +} } // namespace mindspore::api diff --git a/mindspore/ccsrc/cxx_api/python_utils.h b/mindspore/ccsrc/cxx_api/python_utils.h index bbe40f78d4..e7c91a203f 100644 --- a/mindspore/ccsrc/cxx_api/python_utils.h +++ b/mindspore/ccsrc/cxx_api/python_utils.h @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #ifndef MINDSPORE_CCSRC_CXXAPI_PYTHON_UTILS_H #define MINDSPORE_CCSRC_CXXAPI_PYTHON_UTILS_H @@ -22,6 +21,14 @@ void RegAllOp(); bool PythonIsInited(); void InitPython(); void FinalizePython(); -} // namespace mindspore::api +class PythonEnvGuard { + public: + PythonEnvGuard(); + ~PythonEnvGuard(); + + private: + bool origin_init_status_; +}; +} // namespace mindspore::api #endif // MINDSPORE_CCSRC_CXXAPI_PYTHON_UTILS_H