Browse Source

!10565 910 c++ library python env finalize

From: @zhoufeng54
Reviewed-by: @xu-yfei,@kisnwang
Signed-off-by: @xu-yfei
tags/v1.2.0-rc1
mindspore-ci-bot Gitee 5 years ago
parent
commit
990ad3aa14
4 changed files with 29 additions and 8 deletions
  1. +2
    -1
      mindspore/_check_version.py
  2. +6
    -5
      mindspore/ccsrc/cxx_api/graph/ms/ms_graph_impl.cc
  3. +12
    -0
      mindspore/ccsrc/cxx_api/python_utils.cc
  4. +9
    -2
      mindspore/ccsrc/cxx_api/python_utils.h

+ 2
- 1
mindspore/_check_version.py View File

@@ -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']


+ 6
- 5
mindspore/ccsrc/cxx_api/graph/ms/ms_graph_impl.cc View File

@@ -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";


+ 12
- 0
mindspore/ccsrc/cxx_api/python_utils.cc View File

@@ -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

+ 9
- 2
mindspore/ccsrc/cxx_api/python_utils.h View File

@@ -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

Loading…
Cancel
Save