2011/11/3 Li Shen <lis365@gmail.com>
Hi, 

I'm pretty new to jython and I want to use jython to execute many existing python scripts.

For performance reason, I'm wondering if I can pre-compile the python scripts, like below:

private PyCode code = new PythonInterpreter().compile((new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(SCRIPT_NAME))));

And then reuse the compiled code in a multi-thread environment, like below:

public void someMethod() {
    PythonInterpreter interpreter = new PythonInterpreter();
    interpreter.exec(code);
    // ...
}

Looks like the doc doesn't mention if PyCode is thead-safe. Could anyone please give out some clarifications, or suggestions if this approach is incorrect.

Thanks!


I think so. Each PythonInterpreter is an independent state machine.
 
Pierre Thibault

Python Developer/Développeur Python
Montréal, QC
pierre.thibault1@gmail.com