Maxximiliann

Maxximiliann

JSON is serialized into Erlang unicode and then passed to a Python process:

defmodule Sandbox do
  def foo(json_data) do
    port =
      :erlang.open_port(
        {:spawn, 'python3 -u lib/Python/Sandbox.py'},
        [:binary, packet: 4]
      )

    execute = fn data ->
      send(port, {self(), {:command, :erlang.term_to_binary({data})}})

      receive do
        {_, {:data, response}} -> :erlang.binary_to_term(response)
      after
        60000 ->
          {:error, :timeout}
      end
    end

    {_, results_string} = execute.(json_data)

    Print.text("sending JSON data, python result: #{results_string}")
  end
end

Example of what the Python process receives:

json_data = "<<131, 109, 0, 0, 0, 212, 123, 34, 97, 109, 111, 117, 110, 116, 95, 97, 115, 115, 101, 116, 34, 58, 34, 87, 65, 86, 69, 83, 34, 44, 34, 110, 111, 114, 109, 97, 108, 105, 122, 101, 100, 95, 97, 109, 111, 117, 110, 116, 34, 58, 52, 54, 54, 57, 55, 49, 51, 55, 56, 44, 34, 111, 114, 100, 101, 114, 95, 108, 105, 102, 101, 116, 105, 109, 101, 34, 58, 54, 49, 44, 34, 112, 114, 105, 99, 101, 34, 58, 48, 46, 48, 56, 56, 54, 51, 48, 48, 55, 44, 34, 112, 114, 105, 99, 101, 95, 97, 115, 115, 101, 116, 34, 58, 34, 72, 90, 107, 49, 109, 98, 102, 117, 74, 112, 109, 120, 85, 49, 70, 115, 52, 65, 88, 53, 77, 87, 76, 86, 89, 116, 99, 116, 115, 78, 99, 103, 54, 101, 50, 67, 54, 86, 75, 113, 75, 56, 122, 107, 34, 44, 34, 117, 105, 100, 34, 58, 34, 50, 48, 50, 48, 45, 49, 49, 45, 50, 51, 32, 50, 48, 58, 48, 51, 58, 53, 49, 46, 48, 48, 53, 50, 49, 53, 90, 116, 55, 71, 79, 79, 54, 49, 71, 89, 117, 112, 119, 78, 117, 122, 115, 109, 102, 52, 119, 83, 119, 34, 125>>"

To deserialize this into a Python dictionary the “<<” and “>>” characters need to be stripped off, all of the numbers need to be converted from strs to ints, and then the new list of ints needs to be iterated over to decode it into UTF-8 characters.

Is there a more efficient way to pass JSON from Elixir to Python such that it’s readily parsed by a Python process?

First Post! Switch mode

al2o3cr

al2o3cr

As far as the BEAM’s concerned, a port is just a process where you can send bytes with {:command, io_list} and get bytes back by receiving {:data, io_list}. If it’s easier for your program, you can just send those JSON bytes instead of encoding term → binary.

The term conversion process is handy if you’re sending complicated structures that aren’t already encoded. Take a look at erlport, which already handles decoding ETF and has a Protocol abstraction for dispatching to multiple commands through a single port.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

We're in Beta

About us Mission Statement