From: Stefan S. <ssc...@ax...> - 2004-11-24 18:27:44
Attachments:
Server.java
test.py
|
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 -- Stefan Schmieta <ssc...@ax...> |
From: Kent J. <ke...@td...> - 2004-11-25 12:49:42
|
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.core.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); > } > } > } > } |
From: Stefan S. <ssc...@ax...> - 2004-11-29 16:06:05
|
Am Donnerstag, den 25.11.2004, 07:49 -0500 schrieb Kent Johnson: > Your sample files run without error for me using Jython 2.1 and Java > 1.4.2_03-b02 on Windows 2000. Kent, I'm using Jython 2.1 with Java 1.4.2_06-b03 on Windows XP and Linux. The files don't always fail. Maybe one in three runs works ok for me. It seems to be a timing issue between the threads. Also, if I use only one thread, the program always runs successfully. > > 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. The javaSet object is a java.util.TreeSet so it definitely supports iteration. Otherwise I would expect the program to always fail. > > You could try putting this at the beginning of test.py and see what you get: > print javaSet > print type(javaSet) The result of the print statements looks fine to me. Here's the output of a successful run: >>>java -classpath .:/usr/share/java/jython.jar Server [1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 4, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 5, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 6, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 7, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 8, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 9, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] org.python.core.PyJavaInstance [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 4, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 5, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 6, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 7, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 8, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 9, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] org.python.core.PyJavaInstance [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 4, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 5, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 6, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 7, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 8, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 9, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] org.python.core.PyJavaInstance [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 4, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 5, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 6, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 7, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 8, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 9, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] org.python.core.PyJavaInstance [0, 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 4, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 5, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 6, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 7, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 8, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 9, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] org.python.core.PyJavaInstance And here's one where it fails. It doesn't print all five sets because on thread fails and that thread aborts the program with System.exit(1). If I remove the System.exit() call all five sets are printed as in the successful run. >>> java -classpath .:/usr/share/java/jython.jar Server [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 4, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 5, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 6, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 7, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 8, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 9, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] org.python.core.PyJavaInstance [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 4, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 5, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 6, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 7, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 8, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 9, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] org.python.core.PyJavaInstance [1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 4, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 5, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 6, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 7, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 8, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 9, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] org.python.core.PyJavaInstance class org.python.core.PyException Traceback (innermost last): File "test.py", line 3, in ? AttributeError: __getitem__ Thread[Thread-3,5,main]----------- failed! Best, Stefan > > 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.core.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 > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users -- Stefan Schmieta <ssc...@ax...> |