Skip to content

Instantly share code, notes, and snippets.

View martincabrera's full-sized avatar

Martin Cabrera Diaubalick martincabrera

View GitHub Profile
@martincabrera
martincabrera / calling_python_worker.ex
Created February 22, 2021 17:33
Call within our app to PythonWorker
with {:ok, algorithm_result} <- PythonWorker.call(json_input),
{:ok, stacks} <- Jason.decode(algorithm_result) do
{:ok, stacks}
end
@martincabrera
martincabrera / python_worker_handle_call.ex
Created February 22, 2021 17:27
Elixir PythonWorker handle_call function
def handle_call({:optimize, json_input}, _from, pid) do
result = :python.call(pid, :optimize, :optimize, [json_input])
{:reply, {:ok, result}, pid}
end
@martincabrera
martincabrera / python_worker_call.ex
Created February 22, 2021 17:23
Elixir PythonWorker call function
def call(json_input) do
Task.async(fn ->
:poolboy.transaction(
:worker,
fn pid ->
GenServer.call(pid, {:optimize, json_input})
end,
@timeout
)
end)
@martincabrera
martincabrera / poolboy-application.ex
Created February 22, 2021 13:40
Poolboy configuration in application.ex
# Define workers and child supervisors to be supervised
children = [
# Poolboy python initialization
:poolboy.child_spec(:worker, python_poolboy_config())
]
defp python_poolboy_config do
[
{:name, {:local, :worker}},
{:worker_module, Monocle.AutoStacking.PythonWorker},
@martincabrera
martincabrera / poolboy-mix.exs
Created February 22, 2021 13:39
Poolboy configuration in mix.exs
{:poolboy, "~> 1.5"}
@martincabrera
martincabrera / python-call.ex
Created February 22, 2021 13:32
ErlPort Python call example
result = :python.call(pid, :optimize, :optimize, [json_input])
@martincabrera
martincabrera / pyton-pid.ex
Created February 22, 2021 13:22
Python pid
{:ok, pid} = :python.start([{:python_path, to_charlist(path)}, {:python, 'python3'}])
@martincabrera
martincabrera / path.ex
Created February 22, 2021 13:20
Python private directory in Elixir project
path = [:code.priv_dir(:monocle), "python"] |> Path.join()
@martincabrera
martincabrera / erlport-mix.exs
Last active February 22, 2021 13:35
ErlPort configuration in mix.exs
{:erlport, "~> 0.10.1"}