From: @zhoufeng54 Reviewed-by: @xu-yfei,@kisnwang Signed-off-by: @xu-yfeitags/v1.2.0-rc1
| @@ -216,7 +216,8 @@ class AscendEnvChecker(EnvChecker): | |||||
| try: | try: | ||||
| # pylint: disable=unused-import | # pylint: disable=unused-import | ||||
| import te | import te | ||||
| except RuntimeError: | |||||
| # pylint: disable=broad-except | |||||
| except Exception: | |||||
| if Path(self.tbe_path).is_dir(): | if Path(self.tbe_path).is_dir(): | ||||
| if os.getenv('LD_LIBRARY_PATH'): | if os.getenv('LD_LIBRARY_PATH'): | ||||
| os.environ['LD_LIBRARY_PATH'] = self.tbe_path + ":" + os.environ['LD_LIBRARY_PATH'] | os.environ['LD_LIBRARY_PATH'] = self.tbe_path + ":" + os.environ['LD_LIBRARY_PATH'] | ||||
| @@ -123,12 +123,13 @@ Status MsGraphImpl::FinalizeEnv() { | |||||
| return FAILED; | 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; | init_flag_ = false; | ||||
| MS_LOG(INFO) << "End finalize env"; | MS_LOG(INFO) << "End finalize env"; | ||||
| @@ -131,4 +131,16 @@ void FinalizePython() { | |||||
| Py_Finalize(); | Py_Finalize(); | ||||
| } | } | ||||
| } | } | ||||
| PythonEnvGuard::PythonEnvGuard() { | |||||
| origin_init_status_ = PythonIsInited(); | |||||
| InitPython(); | |||||
| } | |||||
| PythonEnvGuard::~PythonEnvGuard() { | |||||
| // finalize when init by this | |||||
| if (!origin_init_status_) { | |||||
| FinalizePython(); | |||||
| } | |||||
| } | |||||
| } // namespace mindspore::api | } // namespace mindspore::api | ||||
| @@ -13,7 +13,6 @@ | |||||
| * See the License for the specific language governing permissions and | * See the License for the specific language governing permissions and | ||||
| * limitations under the License. | * limitations under the License. | ||||
| */ | */ | ||||
| #ifndef MINDSPORE_CCSRC_CXXAPI_PYTHON_UTILS_H | #ifndef MINDSPORE_CCSRC_CXXAPI_PYTHON_UTILS_H | ||||
| #define MINDSPORE_CCSRC_CXXAPI_PYTHON_UTILS_H | #define MINDSPORE_CCSRC_CXXAPI_PYTHON_UTILS_H | ||||
| @@ -22,6 +21,14 @@ void RegAllOp(); | |||||
| bool PythonIsInited(); | bool PythonIsInited(); | ||||
| void InitPython(); | void InitPython(); | ||||
| void FinalizePython(); | 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 | #endif // MINDSPORE_CCSRC_CXXAPI_PYTHON_UTILS_H | ||||