Search in sources :

Example 1 with PythonInterpreter

use of pemja.core.PythonInterpreter 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());
}
Also used : PythonInterpreter(pemja.core.PythonInterpreter) PythonInterpreterConfig(pemja.core.PythonInterpreterConfig) PythonConfig(org.apache.flink.python.PythonConfig) EmbeddedPythonEnvironment(org.apache.flink.python.env.embedded.EmbeddedPythonEnvironment) JobID(org.apache.flink.api.common.JobID)

Aggregations

JobID (org.apache.flink.api.common.JobID)1 PythonConfig (org.apache.flink.python.PythonConfig)1 EmbeddedPythonEnvironment (org.apache.flink.python.env.embedded.EmbeddedPythonEnvironment)1 PythonInterpreter (pemja.core.PythonInterpreter)1 PythonInterpreterConfig (pemja.core.PythonInterpreterConfig)1