From: Robert D. <rdi...@tr...> - 2003-10-09 18:00:02
|
Jeff,=20 Great suggestion on the "throws". One more thing, and BTW I appreciate your help. Can I set python options before constructing the interpreter using System.setProperty? For example: System.setProperty( "python.options.internalTablesImpl", "weak" ); System.setProperty( "python.options.showJavaExceptions", "true" ); System.setProperty( "python.options.showPythonProxyExceptions", "true" ); Or do I need to put it in the registry? R. -----Original Message----- From: Jeff Emanuel [mailto:JEm...@lg...]=20 Sent: Thursday, October 09, 2003 8:04 AM To: Robert DiFalco Subject: RE: [Jython-users] Repeated instantiation of Python Objects in Java Now I understand better. You probably don't want to redefine your classes, but it probably wouldn't have any ill effects, it's just wasteful. I would either define all the classes a priori and omit the py.exec line or create them the first time they were needed, either like your second example, or you could try the eval, and if that throws, then do the exec and eval again. Then you don't need to maintain redundant information that the interpreter maintains anyway. PObject pyo=3Dnull; try { pyo=3Dpy.eval(desc.name+"()"); } catch (PyException ex) { // Should check that ex is a NameError. py.exec(desc.code); pyo=3Dpy.eval(desc.name+"()"); } Creating multiple instances shouldn't cause any leaks, especially since you aren't assigning them to a variable in the interpreter. -----Original Message----- From: Robert DiFalco [mailto:rdi...@tr...] Sent: Wednesday, October 08, 2003 9:41 PM To: Jeff Emanuel Subject: RE: [Jython-users] Repeated instantiation of Python Objects in Java Woops, that was a typo! The code instantiates objects. Like this: PythonInterpreter py =3D PythonInterp.getInstance(); for ( awhile ) { Descriptor desc =3D getRandomPythonImplementationOf = MyInterface.class ); // exec the code (no main, just a single file single class, i.e. foo.py) py.exec( desc.code ); // construct an instance of the object (i.e. pyo =3D foo();) PyObject pyo =3D py.eval( desc.name + "()" ); // basically creates = a null constructor // cast it to an interface it implements MyInterface mi =3D (MyInterface)pyo.__tojava__( MyInterface.class = ); // call a method, garbage collector should clean up resources System.out.println( mi.toString() ); } Basically, I have a bunch of objects in python that I instantiate multiple times from Java. I also do this all in the same interpreter. I'm supposing that I only have to exec the code that contains the object once. Is there any problems with exec'ing it more than once? Otherwise, I suppose I will need to add a state variable something like this: PythonInterpreter py =3D PythonInterp.getInstance(); for ( awhile ) { Descriptor desc =3D getRandomPythonImplementationOf = MyInterface.class ); // make sure it has only been executed once if ( !desc.hasCodeBeenExecuted ) { // exec the code (no main, just a single file single class, i.e. foo.py) py.exec( desc.getClassCode() ); desc.hasCodeBeenExecuted =3D true; } // construct an instance of the object (i.e. pyo =3D foo();) PyObject pyo =3D py.eval( desc.getClassName() + "()" ); // = basically creates a null constructor // cast it to an interface it implements MyInterface mi =3D (MyInterface)pyo.__tojava__( MyInterface.class = ); // call a method, garbage collector should clean up resources System.out.println( mi.toString() ); } =20 Does that make sense? This is actually a rule server, there is really no for loop like this. Basically, I look in the scheduler for different types of rules to execute. These rules are python classes that implement a Java Rule interface. The for loop was just to simulate what I am doing. R. -----Original Message----- From: Jeff Emanuel [mailto:JEm...@lg...]=20 Sent: Wednesday, October 08, 2003 8:25 PM To: Robert DiFalco Subject: RE: [Jython-users] Repeated instantiation of Python Objects in Java In principle, there shouldn't be any leaks with the pseudocode you provided, but I'm not sure what is supposed to do or what your goal is. I think you need to specify what the Jython code looks like. PythonInterpreter.eval only takes a String and you are passing it a PyCode. I don't know why you would exec and then eval the same code object. Jython classes get cached, so if your code defines a new class each time it is executed, you may have a problem. You can specify whether Jython uses soft or weak references for the cache. See the comment in your Jython registry file where it sets python.options.internalTablesImpl. -----Original Message----- From: Robert DiFalco To: jyt...@li... Sent: 10/8/2003 6:58 PM Subject: [Jython-users] Repeated instantiation of Python Objects in Java Is there anything I need to look out for here with regards to memory leaks? PythonInterpreter py =3D PythonInterp.getInstance(); for ( awhile ) { PyCode code =3D getRandomPythonImplementationOf MyInterface.class = ); py.exec( code ); PyObject pyo =3D py.eval( code ); MyInterface mi =3D (MyInterface)pyo.__tojava__( MyInterface.class = ); System.out.println( mi.toString() ); } If the same code is returned and exec'd more than once, does the interpreter know that it's been exec'd before? What actually happens when I call "exec"? Should the code above have memory leaks? TIA, Robert ------------------------------------------------------- This SF.net email is sponsored by: SF.net Giveback Program. SourceForge.net hosts over 70,000 Open Source Projects. See the people who have HELPED US provide better services: Click here: http://sourceforge.net/supporters.php _______________________________________________ Jython-users mailing list Jyt...@li... https://lists.sourceforge.net/lists/listinfo/jython-users |