defmodule The_App.Application do
require Logger
use Application
def start(_type, _args) do
:logger.add_handlers(:The_App)
Print.text("Initiating The_App \n")
children = [
The_AppDB.Repo
]
opts = [strategy: :one_for_one, name: The_App.Supervisor]
Supervisor.start_link(children, opts)
end
end
defmodule Supervisor.PyOperatorManager do
use Supervisor
def start_link(_) do
Supervisor.start_link(__MODULE__, [], name: __MODULE__)
end
@impl true
def init(_) do
children = [
:poolboy.child_spec(:py_pool,
name: {:local, :py_pool},
worker_module: Server.PyOperator,
size: 4,
max_overflow: 2
)
]
Supervisor.init(children, strategy: :one_for_one)
end
def launch() do
:poolboy.transaction(:py_pool, fn pid ->
GenServer.call(pid, {"hello_world", "main", "Hello"})
end)
end
end
defmodule Server.PyOperator do
use GenServer
use Export.Python
def start_link(_) do
GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
end
def init(state) do
Process.flag(:trap_exit, true)
priv_path = Path.join(:code.priv_dir(:the_app), "python")
{:ok, py} = Python.start_link(python_path: priv_path)
{:ok, Map.put(state, :py, py)}
end
def handle_call({py_module, py_lambda, data}, _from, %{py: py} = state) do
result = Python.call(py, py_module, py_lambda, [data])
{:reply, result, state}
end
def terminate(_reason, %{py: py}) do
Python.stop(py)
:ok
end
end
Interactive Elixir (1.12.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Supervisor.PyOperatorManager.launch()
** (exit) exited in: :gen_server.call(:py_pool, {:checkout, #Reference<0.4022030713.2998403074.45553>, true}, 5000)
** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
(stdlib 3.15.1) gen_server.erl:247: :gen_server.call/3
(poolboy 1.5.2) /home/source/Desktop/The_App/deps/poolboy/src/poolboy.erl:63: :poolboy.checkout/3
(poolboy 1.5.2) /home/source/Desktop/The_App/deps/poolboy/src/poolboy.erl:82: :poolboy.transaction/3
iex(1)> Supervisor.PyOperatorManager.start_link([])
** (EXIT from #PID<0.367.0>) shell process exited with reason: shutdown: failed to start child: :py_pool
** (EXIT) an exception was raised:
** (MatchError) no match of right hand side value: {:error, {:already_started, #PID<0.379.0>}}
(poolboy 1.5.2) /home/source/Desktop/The_App/deps/poolboy/src/poolboy.erl:283: :poolboy.new_worker/1
(poolboy 1.5.2) /home/source/Desktop/The_App/deps/poolboy/src/poolboy.erl:304: :poolboy.prepopulate/3
(poolboy 1.5.2) /home/source/Desktop/The_App/deps/poolboy/src/poolboy.erl:153: :poolboy.init/3
(stdlib 3.15.1) gen_server.erl:423: :gen_server.init_it/2
(stdlib 3.15.1) gen_server.erl:390: :gen_server.init_it/6
(stdlib 3.15.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
What exactly is causing these errors and how are they properly addressed?