From: Alan K. <jyt...@xh...> - 2012-10-13 17:35:59
|
> 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. No. 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. Alan. |