Thanks for clearing that up.

So the exec() function call needs to be synchronized - which is what I am concerned with. In my application, I compile the python code once, and then execute is multiple times with different input data.

Now, if I have multiple PythonInterpreter objects each sharing the same PySystemState, can I execute them simultaneously.

I can provide some sample code (SSCE) to demonstrate the exact issue.

On Sat, Oct 13, 2012 at 1:35 PM, Alan Kennedy <> wrote:
> PySystemState although can be shared by multiple different PythonInterpreter
> objects, but then you have to lock the state (synchronize their execution)
> if you have multiple threads.


PySystemState is the component of the runtime environment where
context, e.g. module search path, is stored. PySystemState is what
implements the sys module. So if you import sys and do some inspection
on it, e.g. "dir(sys)", you're looking at a PySystemState object.

PythonInterpreter objects that share a PySystemState will share that
sys module, and no synchronization is necessary for those
PythonInterpresters to share it.

With a single exception: the exec and execfile calls need to be
synchronized. So if you're executing code in a PythonInterpreter to,
for example, provide APIs to embedded code, then the "exec" call
should be executed only by a single thread at a time. But generally
these calls should only ever be executed once anyway, so this is a
minor concern.

> Now in reality, in my specific case, its not the jython that is expensive,
> but other stuff that takes time. So I can still get away with synchronizing
> without affecting performance too much.

Jython is completely threadsafe.

Whether or not your code or its dependencies is also threadsafe is a
completely separate concern.


Santosh Tiwari