Re: [Py4j-users] Calling Python from Java side?
Status: Beta
Brought to you by:
barthe
From: Frank J. I. Jr. <fr...@ae...> - 2015-02-17 17:48:16
|
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 |