use of org.apache.flink.python.env.embedded.EmbeddedPythonEnvironment in project flink by apache.
the class AbstractEmbeddedPythonFunctionOperator method open.
@Override
public void open() throws Exception {
super.open();
pythonConfig = new PythonConfig(config);
pythonEnvironmentManager = createPythonEnvironmentManager();
pythonEnvironmentManager.open();
EmbeddedPythonEnvironment environment = (EmbeddedPythonEnvironment) pythonEnvironmentManager.createEnvironment();
PythonInterpreterConfig interpreterConfig = environment.getConfig();
interpreter = new PythonInterpreter(interpreterConfig);
Map<String, String> env = environment.getEnv();
if (env.containsKey(PYTHON_WORKING_DIR)) {
lock.lockInterruptibly();
try {
JobID jobId = getRuntimeContext().getJobId();
Tuple2<String, Integer> dirAndNums;
if (workingDirectories.containsKey(jobId)) {
dirAndNums = workingDirectories.get(jobId);
} else {
dirAndNums = Tuple2.of(null, 0);
workingDirectories.put(jobId, dirAndNums);
}
dirAndNums.f1 += 1;
if (dirAndNums.f0 == null) {
// get current directory.
interpreter.exec("import os;cwd = os.getcwd();");
dirAndNums.f0 = interpreter.get("cwd", String.class);
String workingDirectory = env.get(PYTHON_WORKING_DIR);
// set working directory
interpreter.exec(String.format("import os;os.chdir('%s')", workingDirectory));
}
} finally {
lock.unlock();
}
}
openPythonInterpreter(pythonConfig.getPythonExec(), env, interpreterConfig.getExecType());
}
Aggregations