Search in sources :

Example 1 with JepConfig

use of jep.JepConfig in project apex-malhar by apache.

the class InterpreterThread method startInterpreter.

/**
 * Starts the interpreter by loading the shared libraries
 * @throws ApexPythonInterpreterException if the interpreter could not be started
 */
public void startInterpreter() throws ApexPythonInterpreterException {
    Thread.currentThread().setName(threadID);
    // To allow for time aware calls
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    loadMandatoryJVMLibraries();
    JepConfig config = new JepConfig().setRedirectOutputStreams(true).setInteractive(false).setClassLoader(Thread.currentThread().getContextClassLoader());
    if (initConfigs.containsKey(PYTHON_INCLUDE_PATHS)) {
        List<String> includePaths = (List<String>) initConfigs.get(PYTHON_INCLUDE_PATHS);
        if (includePaths != null) {
            LOG.info("Adding include path for the in-memory interpreter instance");
            for (String anIncludePath : includePaths) {
                config.addIncludePaths(anIncludePath);
            }
        }
    }
    if (initConfigs.containsKey(PYTHON_SHARED_LIBS)) {
        Set<String> sharedLibs = (Set<String>) initConfigs.get(PYTHON_SHARED_LIBS);
        if (sharedLibs != null) {
            config.setSharedModules(sharedLibs);
            LOG.info("Loaded " + sharedLibs.size() + " shared libraries as config");
        }
    } else {
        LOG.info(" No shared libraries loaded");
    }
    if (initConfigs.containsKey(IDLE_INTERPRETER_SPIN_POLICY)) {
        spinPolicy = SpinPolicy.valueOf((String) initConfigs.get(IDLE_INTERPRETER_SPIN_POLICY));
        LOG.debug("Configuring spin policy to be " + spinPolicy);
    }
    if (initConfigs.containsKey(SLEEP_TIME_MS_IN_CASE_OF_NO_REQUESTS)) {
        sleepTimeMsInCaseOfNoRequests = (Long) initConfigs.get(SLEEP_TIME_MS_IN_CASE_OF_NO_REQUESTS);
        LOG.debug("Configuring sleep time for no requests situation to be " + sleepTimeMsInCaseOfNoRequests);
    }
    try {
        LOG.info("Launching the in-memory interpreter");
        JEP_INSTANCE = new Jep(config);
    } catch (JepException e) {
        // Purposefully logging as this will help in startup issues being captured inline
        LOG.error(e.getMessage(), e);
        throw new ApexPythonInterpreterException(e);
    }
}
Also used : Set(java.util.Set) Jep(jep.Jep) ApexPythonInterpreterException(org.apache.apex.malhar.python.base.ApexPythonInterpreterException) JepConfig(jep.JepConfig) ArrayList(java.util.ArrayList) List(java.util.List) JepException(jep.JepException)

Example 2 with JepConfig

use of jep.JepConfig in project twister2 by DSC-SPIDAL.

the class JepInstance method initialValue.

@Override
protected Jep initialValue() {
    JepConfig jepConfig = new JepConfig();
    jepConfig.setRedirectOutputStreams(true);
    // );
    try {
        TSharedInterpreter jep = new TSharedInterpreter(jepConfig);
        jep.eval("import cloudpickle as cp");
        jep.eval("import base64");
        jep.eval("from abc import ABC, abstractmethod");
        jep.eval("import numpy as np");
        jep.eval("import twister2.utils as utils");
        return jep;
    } catch (JepException jex) {
        throw new Twister2RuntimeException("Couldn't create a JEP instance", jex);
    }
}
Also used : Twister2RuntimeException(edu.iu.dsc.tws.api.exceptions.Twister2RuntimeException) JepConfig(jep.JepConfig) JepException(jep.JepException)

Aggregations

JepConfig (jep.JepConfig)2 JepException (jep.JepException)2 Twister2RuntimeException (edu.iu.dsc.tws.api.exceptions.Twister2RuntimeException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Set (java.util.Set)1 Jep (jep.Jep)1 ApexPythonInterpreterException (org.apache.apex.malhar.python.base.ApexPythonInterpreterException)1