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
Fix:
def main(params_json):
return do_stuff(params_json)
Modification:
- Added explicit
returnkeyword tomain(params_json)method. In contrast to Elixir, return values in Python must be explicitly assigned to thereturnkeyword or they will be discarded.
Note:
- Python does not support hot code reloading. Because files are built as they are imported, restarting
iexis required for code changes to take effect.
1
Popular in Questions
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Hi,
is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
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
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
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
Other popular topics
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
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
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New







