From: <Tri...@we...> - 2007-11-09 15:34:09
|
Hi, I'm running a Jython script using the Java6 ScriptingEngine: ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName("python"); Now when the script runs for the first time, the scripts is loaded, the initialization code is run, imports are processed and everything works fine. What I am trying to do is to run the script again, having it startup and initialize as if it ran for the first time. Unfortunately, no matter what I try, the script always "knows" that it is already loaded and therefore it doesn't execute the initialization code again and doesn't process any imports. I create a new ScriptEngineManager and ScriptEngine when I try to restart a script, but that doesn't work. I tried setting a new ScriptContext and Bindings and deleting all entries from the current Bindings, which doesn't work either. // none of these work ScriptContext newContext = new SimpleScriptContext(); Bindings engineScope = newContext.getBindings(ScriptContext.ENGINE_SCOPE); engine.setBindings(engineScope, ScriptContext.ENGINE_SCOPE); engine.setContext(newContext); Is this a limitation of the JythonScriptingEngine? How do I fresh-start a script that already ran before or run multiple "instances" of the same script? (Without restarting the VM of course). -- View this message in context: http://www.nabble.com/Restarting-a-script-from-scratch-using-Java6-Scripting-API-tf4778360.html#a13669187 Sent from the jython-dev mailing list archive at Nabble.com. |
From: Dave K. <dku...@re...> - 2007-11-09 17:53:58
|
Tristan de Inés <Tristan_de_Ines <at> web.de> writes: > > > Hi, > > I'm running a Jython script using the Java6 ScriptingEngine: > > ScriptEngineManager mgr = new ScriptEngineManager(); > ScriptEngine engine = mgr.getEngineByName("python"); > > Now when the script runs for the first time, the scripts is loaded, the > initialization code is run, imports are processed and everything works fine. > > What I am trying to do is to run the script again, having it startup and > initialize as if it ran for the first time. Unfortunately, no matter what I > try, the script always "knows" that it is already loaded and therefore it > doesn't execute the initialization code again and doesn't process any > imports. > I create a new ScriptEngineManager and ScriptEngine when I try to restart a > script, but that doesn't work. > I tried setting a new ScriptContext and Bindings and deleting all entries > from the current Bindings, which doesn't work either. Not sure whether you have special needs, but ... Is there some reason why you can not creating an instance a Jython interpreter (class PythonInterpreter) directly? Look here for help: http://jython.org/Project/userguide.html#embedding-jython http://wiki.python.org/jython/LearningJython#embedding-jython-into-java If you can use a Jython interpreter in your application, then you can "re- initialize" it by creating a new one. There are other ways of clearing or re-initializing an existing interpreter in limited ways, although clearing out imports might be tricky. You can read a bit about this here: http://wiki.python.org/jython/LearningJython#initializing-the-interpreter By the way, what kind of object is returned by: ScriptEngine engine = mgr.getEngineByName("python"); Is in an instance of PythonInterpreter? I looked at documentation on ScriptEngineManager at http://java.sun.com/javase/6/docs/api/javax/script/ScriptEngineManager.html. The ScriptEngineManager seems to use a ScriptEngineFactory. If the ScriptEngineFactory is returning an instance of PythonInterpreter, then perhaps there is some way to tell that factory to return a *new* instance of PythonInterpreter each time it is called. Hope this helps. Dave |
From: <Tri...@we...> - 2007-11-14 10:52:50
|
I'm now using PythonInterpreter when working with Jython and "restart" a script by creating a new instance, as you mentioned. Interestingly, the script restarts correctly when using PythonInterpreter pi = new PythonInterpreter(); pi.execfile(inputStream); but it still uses the old, previously initialized script when calling a script function using PyFunction func = (PyFunction) pi.get(function, PyFunction.class); func.__call__(pyArgs); even though pi is a new PythonInterpreter instance and after having called pi.execfile() using this new instance. I have to create the new PythonInterpreter instance like this: PyDictionary table = new PyDictionary(); PySystemState sys = new PySystemState(); pi = new PythonInterpreter(table, sys); For the above function call to correctly use the "new" script. Is this a bug? -- View this message in context: http://www.nabble.com/Restarting-a-script-from-scratch-using-Java6-Scripting-API-tf4778360.html#a13744118 Sent from the jython-dev mailing list archive at Nabble.com. |
From: <Tri...@we...> - 2007-11-12 09:49:09
|
Hi Dave, thanks for your answer. I wanted to maintain the possibility of supporting multiple scripting languages, which is why I'm working with the Java Scripting API instead of directly using PythonInterpreter. mgr.getEngineByName("python"); Returns an instance of JythonScriptEngine, which extends from ScriptEngine. Unfortunately the class JythonScriptEngine doesn't help much either. I think it manages a PythonInterpreter and a PySystemState internally but doesn't expose a lot of functionality to it. This is the second time that I'm having difficulties working with Jython because of Java 6 Scripting limitations. If I don't find a workaround for this problem I guess I'll have to consider discarding the Scripting API and work with PythonInterpreter directly. Do you happen to know who is responsible for implementing the individual scripting engines to conform with Java 6 API? Is it the folks at Sun, the Jython developers, or someone else entirely? -- View this message in context: http://www.nabble.com/Restarting-a-script-from-scratch-using-Java6-Scripting-API-tf4778360.html#a13702688 Sent from the jython-dev mailing list archive at Nabble.com. |
From: Oti <oh...@gm...> - 2007-11-14 20:38:00
|
Hi Tristan, the Jython scripting engine was written by a Sun engineer (A. Sundararajan) which I met at past JavaOne conferences. So if you have serious issues which are reproducible, I think he'd be glad to hear, even better if there is a patch. My personal opinion is that we (Jython users and developers) should support him as far as possible, since - hopefully - more and more people will be using Jython through this API. best wishes, Oti. On Nov 12, 2007 10:49 AM, Tristan de In=E9s <Tri...@we...> wrote: > > Hi Dave, thanks for your answer. > > I wanted to maintain the possibility of supporting multiple scripting > languages, which is why I'm working with the Java Scripting API instead o= f > directly using PythonInterpreter. > > mgr.getEngineByName("python"); > > Returns an instance of JythonScriptEngine, which extends from ScriptEngin= e. > Unfortunately the class JythonScriptEngine doesn't help much either. I th= ink > it manages a PythonInterpreter and a PySystemState internally but doesn't > expose a lot of functionality to it. > > This is the second time that I'm having difficulties working with Jython > because of Java 6 Scripting limitations. If I don't find a workaround for > this problem I guess I'll have to consider discarding the Scripting API a= nd > work with PythonInterpreter directly. Do you happen to know who is > responsible for implementing the individual scripting engines to conform > with Java 6 API? Is it the folks at Sun, the Jython developers, or someon= e > else entirely? > -- > View this message in context: http://www.nabble.com/Restarting-a-script-f= rom-scratch-using-Java6-Scripting-API-tf4778360.html#a13702688 > Sent from the jython-dev mailing list archive at Nabble.com. > > > ------------------------------------------------------------------------- > > This SF.net email is sponsored by: Splunk Inc. > Still grepping through log files to find problems? Stop. > Now Search log events and configuration files using AJAX and a browser. > Download your FREE copy of Splunk now >> http://get.splunk.com/ > _______________________________________________ > Jython-dev mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-dev > |