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, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
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
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Other popular topics
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
I would like to know what is the best IDE for elixir development?
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









