Search in sources :

Example 1 with Jep

use of jep.Jep 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 Jep

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

the class AbstractPythonProcessor method initialValue.

@Override
protected Jep initialValue() {
    try {
        // get the jep instance for this thread
        Jep jep = JepInstance.getInstance();
        // todo temporary workaround for JepArray issue.
        // This won't be a significant performance bottleneck though
        String lambdaString = Base64.getEncoder().encodeToString(bytes);
        jep.set("func_bin", lambdaString);
        jep.eval(this.objectId + " = cp.loads(base64.b64decode(func_bin))");
        jep.eval("del func_bin");
        return jep;
    } catch (JepException e) {
        throw new Twister2RuntimeException("Error in building lambda function", e);
    }
}
Also used : Twister2RuntimeException(edu.iu.dsc.tws.api.exceptions.Twister2RuntimeException) Jep(jep.Jep) JepException(jep.JepException)

Aggregations

Jep (jep.Jep)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 JepConfig (jep.JepConfig)1 ApexPythonInterpreterException (org.apache.apex.malhar.python.base.ApexPythonInterpreterException)1