mkorpela
mkorpela

Reputation: 4395

How to communicate with Erlang code from Python code?

Let say I have Erlang "enterprise application":

-module(hello).
-export([start/0]).

start() ->
  spawn(fun() -> loop() end).

loop() ->
 receive
   hello ->
     io:format("Hello, World!~n"),
     loop();

   goodbye ->
     ok
 end.

Is there a way to run it from Python code? So that I can get the "Hello, World!" text back to Python? Erlport seems to work the other way around..

EDIT: In other words how to bind to an Erlang port from Python?

Upvotes: 3

Views: 3228

Answers (4)

Muzaaya Joshua
Muzaaya Joshua

Reputation: 7836

If you need Erlang to execute something within Python, its better you create an escript. The escript is then called by your python Script. The Erlang escript will spit the output you want to your python script if say you pipe them like this:


bash-3.2$ escript Your_Erlang_script.escript | python_script.py

In this way, you keep from doing Port programming, sockets, which may introduce overheads. A simple escript can do what you want and have python communicating with Erlang very well. I have used escripts fro long now and they work with Bash, perl and Lua scripts very well.

OR

we could use command substitution on bash. we create an erlang escript or command. Command meaning something like:

erl -noshell -eval "yourModule:someFunction()" -eval "erlang:halt()"
e.g.
bash-3.2$ h=`erl -noshell -eval "io:format(\"Hello world\",[])" -eval "erlang:halt()"`
bash-3.2$ echo $h
Hello World
bash-3.2$
Now by creating a command substitution as above, we will call out python script to get and consume the output of the erlang command or escript outpu like this

bash-3.2$ python python_script.py $h

Upvotes: -1

user177800
user177800

Reputation:

What you are looking for is Py-Interface, it implements an Erlang compatible Node in Python.

The py_interface provides the possibility to create a node that may be used for communication with other Erlang nodes.

If you have a one-shot command line program that is written in Erlang and just want to execute it and capture the output, just use the subprocess module like you would with any other foreign executable.

Upvotes: 6

driquet
driquet

Reputation: 679

You could use the subprocess module to do that. This is a basic example using that module :

output = Popen(["ls", "-L"], stdout=PIPE).communicate()[0]

The communicate function waits for EOF, so that you can't really communicate in real-time and interact with you app.
Also, the pexpect module can do the same and allow you to interact with the app.

Upvotes: 0

user906780
user906780

Reputation:

There is a python library for erland, import this ;-) http://erlport.org/

hello example:

from erlport import Port, Protocol, String


# Inherit custom protocol from erlport.Protocol
    class HelloProtocol(Protocol):

# Function handle_NAME will be called for incoming tuple {NAME, ...}
    def handle_hello(self, name):
    # String wrapper forces name to be a string instead of a list
    return "Hello, %s" % String(name)


if __name__ == "__main__":
    proto = HelloProtocol()
    # Run protocol with port open on STDIO
    proto.run(Port(use_stdio=True))

Upvotes: 0

Related Questions