Re: [Jython-users] Multiple PythonInterpreter objects and Java threads
by Kent Johnson other posts by this author
Nov 25 2004 12:49PM messages near this date
Re: [Jython-users] TypeError: too many arguments
|
[Jython-users] Executing jython compiled jars
Your sample files run without error for me using Jython 2.1 and Java
1.4.2_03-b02 on Windows 2000.
The error you are seeing says that you are trying to get an indexed
attribute from an object that doesn't support it. This is the error you
would get if for some reason the javaSet object doesn't support iteration.
You could try putting this at the beginning of test.py and see what you get:
print javaSet
print type(javaSet)
HTH,
Kent
Stefan Schmieta wrote:
> Hi,
>
> I'm trying to build a Java server where I can submit Python scripts for
> execution. Since this will run on a multi-processor machine my plan was
> to have a pool of Java threads and when a Python file comes in, I pick
> an idle thread, create a PythonInterpreter object in it and execute the
> Python file.
>
> My test setup is in the two attached files. Server.java creates five
> threads and five PythonInterpreters. In each interpreter I create a
> local variable "javaSet" that is set to a (different) Java TreeSet. Then
> each interpreter executes the test.py file which should simply iterate
> through javaSet. Except that it doesn't. A typical output I get is
>
> class org.python.core.PyException
> class org.python.core.PyException
> Traceback (innermost last):
> File "test.py", line 1, in ?
> AttributeError: __getitem__
> Thread[Thread-4,5,main]----------- failed!
> class org.python.core.PyException
> Traceback (innermost last):
> File "test.py", line 1, in ?
> AttributeError: __getitem__
> Thread[Thread-2,5,main]----------- failed!
> class org.python.core.PyException
> Traceback (innermost last):
> File "test.py", line 1, in ?
> AttributeError: __getitem__
> Thread[Thread-3,5,main]----------- failed!
> Traceback (innermost last):
> File "test.py", line 1, in ?
> AttributeError: __getitem__
> Thread[Thread-1,5,main]----------- failed!
>
> Is the PythonInterpreter class not thread-safe or am I doing something
> stupid here?
>
> Any help is greatly appreciated,
>
> Stefan
>
>
> ------------------------------------------------------------------------
>
> import org.python.util.PythonInterpreter;
> import java.util.Properties;
> import java.util.TreeSet;
>
> public class Server {
> public static void main(String[] args) {
> PythonRunner[] interps = new PythonRunner[5];
> for (int i = 0; i < interps.length; ++i) {
> TreeSet set = new TreeSet();
> for (int j = i; j < 100; ++j) {
> set.add("" + j);
> }
> interps[i] = new PythonRunner();
> interps[i].setSet(set);
> }
> for (int i = 0; i < interps.length; ++i) {
> new Thread(interps[i]).start();
> }
> }
>
> private static class PythonRunner implements java.lang.Runnable {
> private PythonInterpreter interp_ = new PythonInterpreter();
> private String scriptName_ = "test.py";
> public PythonRunner() {
> interp_ = new PythonInterpreter(new org.python.core.PyStringMap(), new org.python.cor
e.PySystemState());
> }
>
> public void setSet(Object set) {
> interp_.set("javaSet", set);
> }
>
> public void setScriptName(String fileName) {
> }
>
> public void run() {
> try {
> interp_.execfile(scriptName_);
> } catch (Exception ex) {
> System.out.println(ex.getClass());
> ex.printStackTrace();
> System.out.println(Thread.currentThread() + "----------- failed!");
> System.exit(1);
> }
> }
> }
> }
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/
_______________________________________________
Jython-users mailing list
Jython-users@[...].net
https://lists.sourceforge.net/lists/listinfo/jython-users
|