Giter Club home page Giter Club logo

java_python_interop's Introduction

Java-Python Interop

Install Pyro

$pip install pyro4

Start Pyro name server

$pyro4-ns

You should get something like this

Not starting broadcast server for localhost.
NS running on localhost:9090 (127.0.0.1)
Warning: HMAC key not set. Anyone can connect to this server!
URI = PYRO:Pyro.NameServer@localhost:9090

Write some Python code to perform remote invocations on using Pyro.

import Pyro4

@Pyro4.expose
class GreetingMaker(object):
    def get_fortune(self, name):
        return "Hello, {0}. Here is your fortune message:\n" \
               "Tomorrow's lucky number is 12345678.".format(name)

daemon = Pyro4.Daemon()                # make a Pyro daemon
ns = Pyro4.locateNS()                  # find the name server
uri = daemon.register(GreetingMaker)   # register the greeting maker as a Pyro object
ns.register("example.greeting", uri)   # register the object with a name in the name server

print("Ready.")
daemon.requestLoop()                   # start the event loop of the server to wait for calls

Execute the above code

python greeting_server.py

You should get the message

Ready.

Write some Java code to use Pyro. Note that this project contains a Maven pom.xml in order to include Pyro dependencies and build an uber-jar with these dependencies.

import net.razorvine.pyro.*;

public class HelloPython {

	public static void main(final String[] args) throws Exception {

		final NameServerProxy ns = NameServerProxy.locateNS(null);
		final PyroProxy remoteobject = new PyroProxy(ns.lookup("example.greeting"));
		final String result = (String) remoteobject.call("get_fortune", "Marek");
		System.out.println(result);
	}
}

Build the Java code

mvn clean package

Test the Java code (after setting up Pyro, starting the name server and the Python server app).

mvn test

This will run the main method. You should see the following on the console

Hello, Marek. Here is your fortune message:
Tomorrow's lucky number is 12345678.

For more information about Pyro, see here. For more information about Pyro in Java, see here.

java_python_interop's People

Contributors

mkolod avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.