Search in sources :

Example 6 with PythonConfig

use of org.apache.flink.python.PythonConfig 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)

Example 7 with PythonConfig

use of org.apache.flink.python.PythonConfig in project flink by apache.

the class AbstractPythonFunctionOperator method open.

@Override
public void open() throws Exception {
    try {
        this.pythonConfig = new PythonConfig(config);
        this.jobOptions = config.toMap();
        this.maxBundleSize = pythonConfig.getMaxBundleSize();
        if (this.maxBundleSize <= 0) {
            this.maxBundleSize = PythonOptions.MAX_BUNDLE_SIZE.defaultValue();
            LOG.error("Invalid value for the maximum bundle size. Using default value of " + this.maxBundleSize + '.');
        } else {
            LOG.info("The maximum bundle size is configured to {}.", this.maxBundleSize);
        }
        this.maxBundleTimeMills = pythonConfig.getMaxBundleTimeMills();
        if (this.maxBundleTimeMills <= 0L) {
            this.maxBundleTimeMills = PythonOptions.MAX_BUNDLE_TIME_MILLS.defaultValue();
            LOG.error("Invalid value for the maximum bundle time. Using default value of " + this.maxBundleTimeMills + '.');
        } else {
            LOG.info("The maximum bundle time is configured to {} milliseconds.", this.maxBundleTimeMills);
        }
        this.elementCount = 0;
        this.lastFinishBundleTime = getProcessingTimeService().getCurrentProcessingTime();
        // Schedule timer to check timeout of finish bundle.
        long bundleCheckPeriod = Math.max(this.maxBundleTimeMills, 1);
        this.checkFinishBundleTimer = getProcessingTimeService().scheduleAtFixedRate(// checkpointing lock
        timestamp -> checkInvokeFinishBundleByTime(), bundleCheckPeriod, bundleCheckPeriod);
    } finally {
        super.open();
    }
}
Also used : PythonOptions(org.apache.flink.python.PythonOptions) ScheduledFuture(java.util.concurrent.ScheduledFuture) Configuration(org.apache.flink.configuration.Configuration) PythonEnvironmentManager(org.apache.flink.python.env.PythonEnvironmentManager) Watermark(org.apache.flink.streaming.api.watermark.Watermark) PythonOperatorUtils.inBatchExecutionMode(org.apache.flink.streaming.api.utils.PythonOperatorUtils.inBatchExecutionMode) FlinkMetricContainer(org.apache.flink.python.metric.FlinkMetricContainer) Field(java.lang.reflect.Field) KeyedStateBackend(org.apache.flink.runtime.state.KeyedStateBackend) BatchExecutionInternalTimeServiceManager(org.apache.flink.streaming.api.operators.sorted.state.BatchExecutionInternalTimeServiceManager) Preconditions(org.apache.flink.util.Preconditions) AbstractStreamOperator(org.apache.flink.streaming.api.operators.AbstractStreamOperator) BatchExecutionKeyedStateBackend(org.apache.flink.streaming.api.operators.sorted.state.BatchExecutionKeyedStateBackend) Objects(java.util.Objects) WrappingRuntimeException(org.apache.flink.util.WrappingRuntimeException) List(java.util.List) ClassLeakCleaner.cleanUpLeakingClasses(org.apache.flink.streaming.api.utils.ClassLeakCleaner.cleanUpLeakingClasses) ChainingStrategy(org.apache.flink.streaming.api.operators.ChainingStrategy) Map(java.util.Map) Internal(org.apache.flink.annotation.Internal) PythonConfig(org.apache.flink.python.PythonConfig) PythonEnv(org.apache.flink.table.functions.python.PythonEnv) PythonConfig(org.apache.flink.python.PythonConfig)

Aggregations

PythonConfig (org.apache.flink.python.PythonConfig)7 Configuration (org.apache.flink.configuration.Configuration)5 Test (org.junit.Test)4 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Internal (org.apache.flink.annotation.Internal)2 PythonOptions (org.apache.flink.python.PythonOptions)2 FlinkMetricContainer (org.apache.flink.python.metric.FlinkMetricContainer)2 KeyedStateBackend (org.apache.flink.runtime.state.KeyedStateBackend)2 Preconditions (org.apache.flink.util.Preconditions)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1