|
From: Ype K. <yk...@xs...> - 2002-01-30 10:07:25
|
Edward, >Hello All, > >My Jython app creates and uses a few org.python.util.PythonInterpreter's. It >looks something like this: > > >|-----Shared namespace ------------| >Main app >Interpreter Interpreter Interpreter > >So the interpreters and the main app share a namespace. > >Can anyone answer these questions: > >o If a script executed within one interpreter alters the state of the main >application (ie, a scripted class is added to some application list), and >the interpreter is deleted, what happens. When the state change is a done in the shared namespace it will stay there when the interpreter is deleted. It is a bit tricky to share namespaces this way. An alternative is to use the namespace of a module shared between the main app and the interpreters. You can set things in a module from outside the module. You could for example import a module in the main app and set a variable in this module from the main app to a shared object. This shared object can then be also be used from the interpreters after they import the module. >Is the interpreter really deleted, or is some reference going to be dangling >about. That depends on how you implement the shared namespace. The system uses a the __dict__ member of a module for the namespace of a module (see module new), and it keeps the modules in sys.modules. >If it is deleted, is the scripted class 'ok', or will weird and unpleasant >things happen. Unpleasant things might happen when you explicitly clear a namespace too early. Only removing references is safer. >o Each interpreter redefines sys.stderr. It seems that stderr is unique >amongst all interpreters within an app. Is there another way to deal with >this? I redefined both sys.stderr via interpreter.exec() calls, and >interpreter.setOut() ... same result. I can think of hacky ways to deal with >this, but there might be an easy solution out there. With a single shared namespace and a single sys.stderr object this is not easy. You'll need some criterion to distinguish the source of the data being sent to sys.stderr. >o Anything else I should be aware of. From some stuff in the archive, I see >only one person saying that they had problems when interpreters were in diff >threads. But no solutions. You could use the thread id as the distinguishing criterion. Ie. replace sys.stderr with an object that uses the thread id to send it's output to different places. See demos/swing/Console.py on changing sys.stdout and sys.stderr. >Sorry if these are basic questions, I have been only using Jython for about >2 months, but am having such a blast that it is my main dev language now. They are not a basic questions, but they pop up often enough that a FAQ entry might be considered. You might consider using the namespace of a new.module() for each interpreter and register this module at sys.modules. You'll have to find an appropriate value for the module name (ie. __name__) to be able to find each module in sys.modules. Only one at a time can be called '__main__', unless you override the lookup in sys.modules to include eg. the thread id. With a namespace per interpreter you can also put a different sys object in each interpreter, but this seems a bit drastic too me. Some modules use their __name__ to find their module in sys.modules (notably PyUnit) and supporting this is nice. When each interpreter thread finishes you can remove all the references from sys.modules to make sure nothing is dangling. I have some bad experience with clearing such namespaces, so i'd recommend to only remove the references. The garbage collector will find non referenced objects in due time. Finally: when you use exec of execfile() you don't need a PyInterpreter. To set things in a module name space before it is started just use module.__dict__['var'] = value. Good luck, Ype -- |