Git Product home page Git Product logo

python-erlastic's Introduction

Erlastic

Usage

Erlastic allows you to serialize/deserialize python objects into erlang binary term.

Basic usage is :

import erlastic
py_struct = erlastic.decode(binary_term)
binary = erlastic.encode(py_struct)

Erlang Port communication usage

The library contains also a function to use python with erlastic in an erlang port to communicate erlang binary term : port_communication() which return (mailbox,port). They are both python coroutines (executed generator) so you can communicate with erlang coroutine using python abstractions :

So for instance, if you want to create a Python server which receives the tuple {A,B} and return {ok,A/B} of {error,divisionbyzero} you can use at the python side :

from erlastic import port_connection,Atom as A
mailbox,port = port_connection()

for (a,b) in mailbox:
  port.send((A("ok"),a/b) if b!=0 else (A("error"),A("divisionbyzero")))

and at the erlang side, use -u python parameter to prevent python output buffering, use 4 bytes packet length because it is the configuration used by the python generators.

Port = open_port({spawn,"python3 -u add_server.py"},[binary,{packet,4}]),
Div = fun(A,B)->
  Port ! {self(),{command,term_to_binary({A,B})}},
  receive {Port,{data,Bin}}->binary_to_term(Bin) after 1000->{error,timeout} end
end,
io:format("send {A,B}=~p, python result : ~p~n",[{32,10},Div(32,10)]),
io:format("send {A,B}=~p, python result : ~p~n",[{2,0},Div(2,0)]),
io:format("send {A,B}=~p, python result : ~p~n",[{1,1},Div(1,1)])

or in elixir :

port = Port.open({:spawn,'python3 -u add_server.py'},[:binary|[packet: 4]])
div = fn(a,b)->
  port <- {self,{:command,term_to_binary({a,b})}}
  receive do {_,{:data,b}} -> binary_to_term(b) after 100->{:error,:timeout} end
end
IO.puts "send {a,b}={32,10}, python result : #{inspect div.(32,10)}"
IO.puts "send {a,b}={2,0}, python result : #{inspect div.(2,0)}"
IO.puts "send {a,b}={1,1}, python result : #{inspect div.(1,1)}"

python-erlastic's People

Contributors

apanda avatar awetzel avatar hntrmrrs avatar ichernetsky avatar lukebakken avatar samuel avatar yosh avatar

Watchers

 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.