Maxximiliann

Maxximiliann

Export(erlport) :undefined Error

defmodule Supervisor.PyOperatorManager do
  use Supervisor

  def start_link(_) do
    Supervisor.start_link(__MODULE__, [], name: __MODULE__)
  end

  @impl true
  def init(_) do
    Process.flag(:trap_exit, true)

    children = [
      :poolboy.child_spec(:py_pool,
        name: {:local, :py_pool},
        worker_module: Server.PyOperator,
        size: 10,
        max_overflow: 5
      )
    ]

    Supervisor.init(children, strategy: :one_for_one)
  end

  def launch(data \\ [], py_module, py_lambda) do
    :poolboy.transaction(:py_pool, fn pid ->
      GenServer.call(pid, {data, py_module, py_lambda}, 30_000)
    end)
  end
end
defmodule Server.PyOperator do
  use GenServer
  use Export.Python

  def start_link(_) do
    GenServer.start_link(__MODULE__, %{})
  end

  @impl true
  def init(state) do
    Process.flag(:trap_exit, true)

    priv_path = Path.join(:code.priv_dir(:arbit), "python")
    {:ok, py} = Python.start_link(python_path: priv_path)
    {:ok, Map.put(state, :py, py)}
  end

  @impl true
  def handle_call({data, py_module, py_lambda}, _from, %{py: py} = state) do
    results = Python.call(py, py_module, py_lambda, [data])

    results
    |> IO.inspect(label: "#{__MODULE__} - line 23")

    {:reply, results, state}
  end

  @impl true
  def terminate(_reason, %{py: py}) do
    Python.stop(py)
    :ok
  end
end
#foo.py
import simplejson as json
from erlport.erlterms import Atom
from heavy_processes import some_heavy_process
from some_lib.errors import (AuthenticationError, PermissionDenied, ArgumentsRequired, BadRequest, 
                        BadResponse, NullResponse, NotSupported, NetworkError, DDoSProtection,
                        RateLimitExceeded, OnMaintenance, InvalidNonce, RequestTimeout)


def transmit_report(success_status, report, id, category, misc):
    success_status_as_bytes = bytes(success_status, encoding='utf8')
    full_report = report | {
        'id': id, 'category': category, 'misc?': misc}
    full_report_json = json.dumps(full_report)

    return (Atom(success_status_as_bytes), (full_report_json))


def do_stuff(params_json):
    params = json.loads(params_json)

    id = params["id"]
    category = params["category"]
    misc = params["misc"]

    arg1 = params["arg1"]
    arg2 = params["arg2"]
    arg3 = params["arg3"]
    arg4 = params["arg4"]
    arg5 = params["arg5"]

    try:
        report = some_heavy_process(
            arg1, arg2, arg3, arg4, arg5)

    except (AuthenticationError, PermissionDenied, ArgumentsRequired, BadRequest, BadResponse,
            NullResponse, NotSupported, NetworkError, DDoSProtection,
            RateLimitExceeded, OnMaintenance, InvalidNonce, RequestTimeout) as error:

        transmit_report(
            'error', {'error': str(error)}, id, category, misc)
    except Exception as crash_report:
        transmit_report('error', {'error': str(
            crash_report)}, id, category, misc)
    else:
        transmit_report(
            'ok', report, id, category, misc)


def main(params_json):
    do_stuff(params_json)


if __name__ == '__main__':
    main(params_json)

Issue:
Calling Supervisor.PyOperatorManager.launch(params_json, "foo", "main") only returns :undefined instead of the tuple from transmit_report.

How is this issue resolved?

Marked As Solved

Maxximiliann

Maxximiliann

Fix:

def main(params_json):
    return do_stuff(params_json)

Modification:

  • Added explicit return keyword to main(params_json) method. In contrast to Elixir, return values in Python must be explicitly assigned to the return keyword or they will be discarded.

Note:

  • Python does not support hot code reloading. Because files are built as they are imported, restarting iex is required for code changes to take effect.

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement