|
View:
New views
3 Messages
—
Rating Filter:
Alert me
|
|
|
Jython concurrency in the PythonInterpreter classI am investigating the use of Jython to replace a simple home-grown
scripting language for a Java application. I'm instantiating a PythonInterpreter and executing an entire jython file at once, like this: PythonInterpreter interp = new PythonInterpreter(); interp.execfile("test.py"); It works beautifully for sequential execution of Java method calls, but I'm uncertain as to the best way to implement concurrent execution. In my first attempt, using threads, "test.py" included code like this: from threading import Thread Thread(JavaClassA.methodA()).start() Thread(JavaClassB.methodB()).start() This is a simple case with no shared data between methodA and methodB. Both methods write to a log4j log file. The entries in the log file appear sequentially, labeled as running on thread "MainThread". That is, the results are the same as if I just do this: JavaClassA.methodA() JavaClassB.methodB() Is the PythonInterpreter class capable of creating new threads? Is there a better technique for concurrency that I should use instead? Any comments are greatly appreciated. Thanks. Ted ------------------------------------------------------------------------------ Enter the BlackBerry Developer Challenge This is your chance to win up to $100,000 in prizes! For a limited time, vendors submitting new applications to BlackBerry App World(TM) will have the opportunity to enter the BlackBerry Developer Challenge. See full prize details at: http://p.sf.net/sfu/Challenge _______________________________________________ Jython-users mailing list Jython-users@... https://lists.sourceforge.net/lists/listinfo/jython-users |
|
|
Re: Jython concurrency in the PythonInterpreter classHi Ted,
On Wed, Jul 8, 2009 at 1:32 PM, Ted Larson Freeman<freeman@...> wrote: > In my first attempt, using threads, "test.py" included code > like this: > > from threading import Thread > Thread(JavaClassA.methodA()).start() > Thread(JavaClassB.methodB()).start() Check out the docs for the Thread object: http://docs.python.org/library/threading.html#thread-objects The first argument is the thread group, so you're setting the thread group for your threads with the results of methodA and methodB. Since you're calling methodA and methodB to create arguments for Thread, they're run in the main thread. What you want to do is set the target of the Thread to the function or method you want invoked on the new thread, and then call start. For example, the code below calls add on an ArrayList on a separate thread: Jython 2.5.0+ (trunk:6503:6505M, Jul 8 2009, 18:36:14) [Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)] on java1.6.0_13 Type "help", "copyright", "credits" or "license" for more information. >>> from threading import Thread >>> from java.util import ArrayList >>> l = ArrayList() >>> Thread(target=l.add, args=(5,)).start() >>> l [5] The args keyword arg is a tuple of positional arguments to pass to the target method. With your examples, you'd want to do Thread(target=JavaClassA.methodA).start() and Thread(target=JavaClassB.methodB).start(). Hope this helps, Charlie ------------------------------------------------------------------------------ Enter the BlackBerry Developer Challenge This is your chance to win up to $100,000 in prizes! For a limited time, vendors submitting new applications to BlackBerry App World(TM) will have the opportunity to enter the BlackBerry Developer Challenge. See full prize details at: http://p.sf.net/sfu/Challenge _______________________________________________ Jython-users mailing list Jython-users@... https://lists.sourceforge.net/lists/listinfo/jython-users |
|
|
Re: Jython concurrency in the PythonInterpreter classThanks for the clear and amazingly prompt reply, Charlie!
That got me going. At first I was troubled by seeing all of my new threads labeled "Thread" in the log4j output (is this a bug?). I found that by assigning my own names to the threads I could see each thread executing as expected. I ended up with this: t1 = Thread(target=JavaClassA.methodA, name='t1') t2 = Thread(target=JavaClassB.methadB, name='t2') t1.start() t2.start() t1.join() t2.join() Thanks again, Ted On Wed, Jul 8, 2009 at 6:46 PM, Charlie Groves<charlie.groves@...> wrote: > Hi Ted, > > On Wed, Jul 8, 2009 at 1:32 PM, Ted Larson > Freeman<freeman@...> wrote: >> In my first attempt, using threads, "test.py" included code >> like this: >> >> from threading import Thread >> Thread(JavaClassA.methodA()).start() >> Thread(JavaClassB.methodB()).start() > > Check out the docs for the Thread object: > http://docs.python.org/library/threading.html#thread-objects The > first argument is the thread group, so you're setting the thread group > for your threads with the results of methodA and methodB. Since > you're calling methodA and methodB to create arguments for Thread, > they're run in the main thread. > > What you want to do is set the target of the Thread to the function or > method you want invoked on the new thread, and then call start. For > example, the code below calls add on an ArrayList on a separate > thread: > > Jython 2.5.0+ (trunk:6503:6505M, Jul 8 2009, 18:36:14) > [Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)] on java1.6.0_13 > Type "help", "copyright", "credits" or "license" for more information. >>>> from threading import Thread >>>> from java.util import ArrayList >>>> l = ArrayList() >>>> Thread(target=l.add, args=(5,)).start() >>>> l > [5] > > The args keyword arg is a tuple of positional arguments to pass to the > target method. > > With your examples, you'd want to do > Thread(target=JavaClassA.methodA).start() and > Thread(target=JavaClassB.methodB).start(). > > Hope this helps, > Charlie > > ------------------------------------------------------------------------------ > Enter the BlackBerry Developer Challenge > This is your chance to win up to $100,000 in prizes! For a limited time, > vendors submitting new applications to BlackBerry App World(TM) will have > the opportunity to enter the BlackBerry Developer Challenge. See full prize > details at: http://p.sf.net/sfu/Challenge > _______________________________________________ > Jython-users mailing list > Jython-users@... > https://lists.sourceforge.net/lists/listinfo/jython-users > > > ------------------------------------------------------------------------------ Enter the BlackBerry Developer Challenge This is your chance to win up to $100,000 in prizes! For a limited time, vendors submitting new applications to BlackBerry App World(TM) will have the opportunity to enter the BlackBerry Developer Challenge. See full prize details at: http://p.sf.net/sfu/Challenge _______________________________________________ Jython-users mailing list Jython-users@... https://lists.sourceforge.net/lists/listinfo/jython-users |
| Free embeddable forum powered by Nabble | Forum Help |