You are correct. The javadoc for PySystemState's add_XXX methods state
this (see add_classdir):
http://www.jython.org/docs/javadoc/org/python/core/PySystemState.html
They work as advertised inside Tomcat.
As for another approach, you can manually append to the jvm's classpath
via java.lang.System.setProperty():
// from a static context
System.setProperty("java.class.path",
System.getProperty("java.class.path") +
System.getProperty("path.separator") +
"c:\\mydir");
Then, programmatically append to the jython sys path:
PySystemState sys = new PySystemState();
sys.path.append(new PyString("c:\\mydir"));
Create the interpreter using the sys instance:
PythonInterpreter interp = new PythonInterpreter(null, sys);
And you should be able to load classes from packages under mydir
the standard way. If you can't programatically alter the jvm's
classpath (SecurityManager issue, considered bad form, whatever),
I think you'll have to do your own classloader. But Samuele Pedroni
might know something else to try.
Good luck,
Clark
-----Original Message-----
From: Stefan Baramov [mailto:Stefan.Baramov@...]
Sent: Thursday, July 17, 2003 12:10 PM
To: 'jython-users@...'
Subject: RE: [Jython-users] Memory leaks (?!) with multiple instances of
P ythonInterpreter
Hi Clark,
I am trying to follow your approach but I am facing a problem with loading
classes outside of the system class path. In your email, you are doing :
PySystemState.add_classdir(classesHome);
But this does not create a custom class loader that will load your classes.
The Python Interpreter will relay on the system class loader to do this.
Same for
PySystemState.packageManager.addJar("C:\\my_folder\\mylib.jar",
true);
This will resolved the import statements ("from mylib import MyClass" will
work) but if you try to instantiate the actual class the interpreter will
fail with NullPointerException. After thorough research I found that neither
"add_classdir" nor "addJar" will create a custom class loader to load these
classes. Generally you would have to do something like this:
PySystemState sys = new PySystemState();
sys.setClassLoader(myClassLoader);
interp = new PythonInterpreter(null, sys);
So first question: Am I right? Is this correct?
Second question: Since I am not too keen on writing my own class loader, is
there another approach?
Thanks,
Stefan
-----Original Message-----
From: Updike, Clark [mailto:Clark.Updike@...]
Sent: Thursday, June 26, 2003 10:45 AM
To: 'jython-users@...'
Subject: RE: [Jython-users] Memory leaks (?!) with multiple instances of
P ythonInterpreter
Importance: High
I think Michel Pelletier hit it on the head with the advice to create
a new PySystemState and pass it into the PythonInterpreter constructor.
My app sets up the PySystemState in a static context as follows:
static {
...
PythonInterpreter.initialize(overriddenProps, overridingProps, new
String[0]);
PySystemState.initialize();
PySystemState.add_classdir(classesHome);
PySystemState.add_extdir(pythonHome, true);
...
}
Then when I create the interpreter instance (typically one per web request):
interp = new PythonInterpreter(null, new PySystemState());
If you are doing this kind of embedding, a good reference is Robert Bill's
"Jython for Java Programmers". If you don't create a new PySystemState, I
believe your interpreters will share a single PySystemState. If every
script calls sys.path.append('/some/path'), I believe your path will a
'/some/path' for every script invocation. So you could run out of memory
by this kind of problem (I'm sure there are many more severe resource leaks
you could create than just sys.path.append).
My application runs interpreters in this manner (inside the Tomcat
servlet container) in production for dozens of scripts. We have left
our jvm running for weeks at a time with no memory problems.
-Clark
-----Original Message-----
From: Carl Rischar [mailto:carl@...]
Sent: Thursday, June 26, 2003 10:03 AM
To: Vlad (diz) Mereuta
Cc: jython-users@...
Subject: Re: [Jython-users] Memory leaks (?!) with multiple instances of
PythonInterpreter
I ran into a similiar issue. Growth over time. Then I realized the script
was the issue. Realize if the script creates objects that are held onto by
system objects
the potentially interpereters will not be garbage collected. I had an
issue with not properly closing prepared statements for jdbc. If not
closed they will be retained and not garbage collected.
That may not be phrased correctly. But the issue could be the script
having references back to your host application. After I corrected the
closure issue memory management seemed fine.
Regards
Carl
On Thu, 26 Jun 2003, Vlad (diz) Mereuta wrote:
> Hello all,
>
> While trying to replace a custom (and terribly ugly) scripting language in
> of my company's applications with Jython, I ran into a major problem.
>
> The setup is as follows. The java program creates a number of threads,
> which are executed at various time intervals (as frequently as 2 seconds,
> however most are around 1-2 minutes or more). Each thread would attempt to
> execute the following code:
> org.python.util.PythonInterpreter py_interpreter = new
> org.python.util.PythonInterpreter();
> (...)
> py_interpreter.execfile(getPython_parser());
> py_interpreter.cleanup();
> py_interpreter = null;
>
> That is the application would execfile a series of python scripts, over
> and over again. I am not loading the files into memory and not caching the
> interpreters as I want to pick up any changes in the scripts on the fly
> without having to restart the application. Also there is a good chance
> that the same script would have to be ran independently by two different
> threads.
>
> The problem: within 24 hours, the application inevitably dies, after
> running out of memory. I have tried different settings for the VM (last
> one was -Xms512m -Xmx512m -XX:NewRatio=2 -XX:SoftRefLRUPolicyMSPerMB=250)
> with no luck.
>
> I have also tried some of the things suggested in various places such as
> setting python.options.internalTablesImpl to soft and importing and
> reloading a dummy (empty) python script. Still no luck.
>
> I should also mention that if I remove the python related code, the
> application runs comfortably in 24M of memory.
>
> This problem is present in both the latest CVS build and in 2.1.
>
> Any ideas how I can get around it?
>
> Best regards,
> Vlad
>
>
> Random Thought:
> --------------
>
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by: INetU
> Attention Web Developers & Consultants: Become An INetU Hosting Partner.
> Refer Dedicated Servers. We Manage Them. You Get 10% Monthly Commission!
> INetU Dedicated Managed Hosting http://www.inetu.net/partner/index.php
> _______________________________________________
> Jython-users mailing list
> Jython-users@...
> https://lists.sourceforge.net/lists/listinfo/jython-users
>
--
Regards,
Carl
-------------------------------
carl@...
rischar@...
The only person who always got his work done by Friday was Robinson Crusoe.
-------------------------------------------------------
This SF.Net email is sponsored by: INetU
Attention Web Developers & Consultants: Become An INetU Hosting Partner.
Refer Dedicated Servers. We Manage Them. You Get 10% Monthly Commission!
INetU Dedicated Managed Hosting http://www.inetu.net/partner/index.php
_______________________________________________
Jython-users mailing list
Jython-users@...
https://lists.sourceforge.net/lists/listinfo/jython-users
-------------------------------------------------------
This SF.Net email is sponsored by: INetU
Attention Web Developers & Consultants: Become An INetU Hosting Partner.
Refer Dedicated Servers. We Manage Them. You Get 10% Monthly Commission!
INetU Dedicated Managed Hosting http://www.inetu.net/partner/index.php
_______________________________________________
Jython-users mailing list
Jython-users@...
https://lists.sourceforge.net/lists/listinfo/jython-users
-------------------------------------------------------
This SF.net email is sponsored by: VM Ware
With VMware you can run multiple operating systems on a single machine.
WITHOUT REBOOTING! Mix Linux / Windows / Novell virtual machines at the
same time. Free trial click here: http://www.vmware.com/wl/offer/345/0
_______________________________________________
Jython-users mailing list
Jython-users@...
https://lists.sourceforge.net/lists/listinfo/jython-users
|