Re: [Py4j-users] Calling Python from Java side?
Status: Beta
Brought to you by:
barthe
From: Barthelemy D. <bar...@in...> - 2015-02-17 09:33:01
|
Hi Frank, Py4J can be used to call Python code from Java, but because of the static nature of Java and the design of Py4J this can be only achieved through Java interfaces. Py4J enables a communication channel between a Python interpreter and a JVM. Usually the control flow is driven by Python, but you could certainly spawn a Python interpreter from Java and control it with some synchronization. Because Python is dynamic, it does not need to create in advance all possible interfaces/classes present in the JVM and it can dynamically wrap any Java instance. On the Java side, Java needs to have access to all interfaces at compilation time so Py4J requires that Python code implements a Java interface to be used by the Java side. 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). 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) Then, once you are done, the control flow would be: 1. Start a Python interpreter (this could be started from your Java program) 2. Connect the Python interpreter to your JVM (i.e., with JavaGateway(...)) 3. In Python, create an instance of the class MySciPyImpl 4. In Python, call PythonProxy.setMySciPy(myscipyimpl_instance) 5. In Python, notifies your Java code that it can take control (e.g., by calling a run loop, waking up a thread, etc.) 6. Your Java code can now call Python with PythonProxy.getMySciPy().yourMethodHere() There are some limitations: the Python code cannot send Python objects to the Java side, it can only send primitives or references to Java objects. It could maintain references to Python objects that would be accessed through the other methods of your MySciPy interface though. Some workarounds: a) You could call eval or exec in your Python code. From Java, you would then just send strings. b) I'm open to feature requests such as automatically wrapping Python collections to Java interfaces (e.g., you could return a Python sequence and it would appear to be a java.util.List on the Java side). HTH, Barthelemy On Tue, Feb 17, 2015 at 2:21 AM, Stephen Boesch <ja...@gm...> wrote: > HI py4j is for going the other way: the Gateway serves java classes for use > by python. > > 2015-02-16 20:38 GMT-08:00 Frank J. Iannarilli, Jr. <fr...@ae...>: >> >> Hi, >> >> >> Barthelemy, thanks for contributing Py4J! >> >> Is Py4J suitable for enabling me to call Python methods (e.g. SciPy >> library) from my Java program? >> >> I've succeeded in exercising the Getting Started example: >> http://py4j.sourceforge.net/getting_started.html >> >> I am trying to grok your example: >> >> http://py4j.sourceforge.net/advanced_topics.html#implementing-java-interfaces-from-python-callback >> which was pointed to in response to a similar StackOverflow question: >> >> http://stackoverflow.com/questions/23157424/py4j-how-would-i-go-about-on-calling-a-python-method-in-java?rq=1 >> >> Although I can see (from the OperatorExample.java file in the >> distribution) that Java File2 is not showing this method: >> public static void main(String[] args) { >> GatewayServer server = new GatewayServer(new OperatorExample()); >> server.start(); >> } >> , >> I still can't see from this example how to call Python functions from my >> Java program. Where this example (final lines in Python code) show: >> # "Sends" python object to the Java side. >> numbers = operator_example.randomBinaryOperator(operator) >> the meaning of "Sends" seems to imply a "non-stop round-trip" starting >> from and ending on the Python side. >> >> Is Py4J the wrong tool to be using for what I'm seeking? Any advice >> appreciated! >> Thanks, >> Frank >> >> >> >> -- >> >> 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 >> > > > ------------------------------------------------------------------------------ > 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 > |