!10565 910 c++ library python env finalize

From: @zhoufeng54
Reviewed-by: @xu-yfei,@kisnwang
Signed-off-by: @xu-yfei
pull/10565/MERGE
mindspore-ci-bot 4 years ago committed by Gitee
commit 990ad3aa14

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

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

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

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

Loading…
Cancel
Save