Pyrlang¶
Pyrlang makes a Python program a node of an Erlang cluster. It speaks the
Erlang distribution protocol on asyncio, so Erlang, Elixir and other BEAM
languages reach Python code with what they already use: messages to pids
and registered names, links, monitors, gen_server:call, rpc:call,
spawn/4.
Python processes with Erlang-compatible pids, registered names and aliases; messages in both directions.
Links and monitors between Python and Erlang processes, both ways.
rpc:call,erpcandspawnfrom Erlang into Python; aGenServerclass that answersgen_server:callandcast.Talks to Erlang/OTP 25 to 29. Needs Python 3.11 or newer.
Three projects work together and can be used apart:
Pyrlang: this project, the node.
Term: Erlang terms as Python values and the External Term Format codec, in Python and in Rust. Pyrlang uses it for every message.
Epmd: an Erlang Port Mapper Daemon in Python, for hosts without Erlang.
A first node¶
class MyProcess(Process):
def __init__(self) -> None:
super().__init__()
self.get_node().register_name(self, Atom("my_process"))
def handle_one_inbox_message(self, msg: Any) -> None:
# A message carries no sender: Erlang puts self() into the message.
if isinstance(msg, tuple) and len(msg) == 2:
sender, text = msg
self.get_node().send_nowait(
sender=self.pid_, receiver=sender, message=(Atom("echo"), text)
)
async def main(node_name: str = "py@127.0.0.1", cookie: str = "COOKIE") -> None:
node = Node(node_name=node_name, cookie=cookie)
try:
await node.wait_started()
MyProcess()
await asyncio.Event().wait() # serve until cancelled
finally:
node.destroy()
From an Erlang shell started with
erl -name erl@127.0.0.1 -setcookie COOKIE:
(erl@127.0.0.1)1> {my_process, 'py@127.0.0.1'} ! {self(), <<"hi">>}.
(erl@127.0.0.1)2> flush().
Shell got {echo,<<"hi">>}
Getting started goes through this and the other examples.