Re: [Py4j-users] Calling Python from Java side?
Status: Beta
Brought to you by:
barthe
From: Barthelemy D. <bar...@in...> - 2015-02-18 10:36:54
|
Hi Frank, pyrolite might be suitable for your needs. It runs a server on the python interpreter and definitively do some dynamic evaluation and type conversion. You could achieve something similar with Py4J, but as you noticed, there are more steps involved. The steps I outlined were indeed the same as the Operator example with a few more annotations :-) For example, OperatorExample could have a field, private Operator operator; with a getter and setter. >From Python, you could call: addition = Addition() ... myOperatorExample.setOperator(addition) # From now on, the Java side has a reference to your Python class. # Call a method that will tell the Java side that it is now in control gateway.jvm.yourpackage.YourControllerClass.youHaveControl() Barthelemy On Tue, Feb 17, 2015 at 12:48 PM, Frank J. Iannarilli, Jr. <fr...@ae...> wrote: > On 2/17/2015 4:32 AM, Barthelemy Dagenais wrote: >> >> The process is a little involved so there might be better alternatives >> (jython would probably not work with SciPy. not sure how pyjnius >> handles Java to Python). > > Hi Barthelemy, > > Thanks for your suggestions, to which I have a few follow-up questions. > > But first, do you have any opinions about another possible alternative, that > is to use Pyrolite? > https://pythonhosted.org/Pyro4/pyrolite.html > {pyrolite reportedly eliminates the Jython hop earlier required when using > the following chain: > Java<->Jython<->Pyro<->CPython; so now Java<->pyrolite<->CPython} > .... > > So if I understand your steps below, they are nearly parallel to your > example in > http://py4j.sourceforge.net/advanced_topics.html#implementing-java-interfaces-from-python-callback > > as follows below, and if so, I might simply first attempt to extend your > example: > >> The steps would be: >> >> 1. In Java, declare an interface (e.g., MySciPy). >> 2. In Java, create a class that will hold an instance of MySciPy. This >> is the class you will use in Java to "talk to Python through MySciPy". >> We will call it PythonProxy with setMySciPy and getMySciPy >> 3. In Python, implement the interface in a class (e.g., MySciPyImpl) > > > 1. MySciPy=interface Operator (Java side) > 2. PythonProxy=OperatorExample (Java side) > --> "holding instance of > MySciPy"=OperatorExample.randomBinaryOperator(Operator op) > 3. MySciPyImpl=Addition (Python side) > >> Then, once you are done, the control flow would be: >> ... >> 4. In Python, call PythonProxy.setMySciPy(myscipyimpl_instance) >> ... >> 6. Your Java code can now call Python with >> PythonProxy.getMySciPy().yourMethodHere() > > > 4. In Python, call PythonProxy.setMySciPy(myscipyimpl_instance) <=> > gateway.entry_point.randomBinaryOperator(operator) > OR > gateway.jvm.py4j.examples.OperatorExample().randomBinaryOperator(operator) > > 6. yourMethodHere() = doOperation() > > > But now to achieve what I'm seeking, which is your Step 6, I'm still > mystified but would the following get me close?: > > On Step2, in my Java client code, I would also do: > myOperatorExample = new OperatorExample(); > GatewayServer server = new GatewayServer(myOperatorExample); > server.start(); > > so that: > > 6. In Java, PythonProxy.getMySciPy().yourMethodHere() <=> > numbers = myOperatorExample.randomBinaryOperator(Operator.doOperation) > > I don't see how the above would bind myOperatorExample to the actual Python > proxy. > > > Thanks! > > > > > > > -- > > Frank J. Iannarilli fr...@ae... > Aerodyne Research, Inc., 45 Manning Road, Billerica, MA 01821 USA > > > > ------------------------------------------------------------------------------ > Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server > from Actuate! Instantly Supercharge Your Business Reports and Dashboards > with Interactivity, Sharing, Native Excel Exports, App Integration & more > Get technology previously reserved for billion-dollar corporations, FREE > http://pubads.g.doubleclick.net/gampad/clk?id=190641631&iu=/4140/ostg.clktrk > _______________________________________________ > Py4j-users mailing list > Py4...@li... > https://lists.sourceforge.net/lists/listinfo/py4j-users > |