Hello,
I am planning to use Jep to run multiple functions concurrently.
From the wiki I understood that Jep uses sub interpreters for every new Jep instance, and that such Jep instances must be accessed only by the thread that instantiated them. If I understood correctly, the recommended usage pattern consists in instantiating a Jep object, using it within the current thread, and closing it when done.
In my early tests I was able to invoke functions as follows (please advice if I am doing something wrong, I could not find a complete example in the wiki and in the Java tests);
- Instantiate Jep passing in the includePaths populated to list the file system folders containing the custom python modules that are imported by the "top level" module where the function to be invoked is declared
- perform a runScript() passing in the full path to the top level module
- invoke the function
Jep jep = new Jep(false, "/home/user/modules");
jep.runScript("/home/user/modules/hello.py");
Object result = jep.invoke("hello_jep");
jep.close();
My application will be a Java Enterprise solution used concurrently by hundreds of users. Each of them will perform some action that will require invoking at least a python function to perform a numerical computation. As adviced in the wiki, I will be invoking python functions, and will not use any global variable to avoid leaks and to keep the Jep instances as much isolated as possible.
Eventually, I will have tenths, possibly hundreds, of different functions and on the live application, hundreds of users will be invoking such functions concurrently.
For example I may have f1() f2() f3() .... f100() functions and u1.... u300 users. In a particular moment in time, there may be users u1..u20 calling f1(), u21...u70 calling f3(), and remaining users calling a different function each.
In such a scenario, the sample code I wrote above would make Jep read "hello.py" for every function invocation. This would result in a lot of disk I/O and in general a potential waste of CPU time.
Is there a way to globally preload all the python files and then re-use them in a thread-safe way?
Alternatively, would you recommend to pool Jep instances (one for each python function)?
GianMaria.