defmodule Server.Bar do
use GenServer
use Export.Python
require Logger
@ms_sleep_interval 500
def start_link(_) do
GenServer.start_link(__MODULE__, %{})
end
@impl true
def init(pid) do
Process.flag(:trap_exit, true)
Print.text("Launching #{__MODULE__} . . . ")
Logger.info("Launching #{__MODULE__} . . . ")
Supervisor.PyOperatorManager.launch([], "baz", "main")
|> Foo.main()
Process.send_after(self(), :tick, @ms_sleep_interval)
{:ok, pid}
end
@impl true
def handle_info(:tick, state) do
Process.send_after(self(), :tick, @ms_sleep_interval)
Print.text("Running #{__MODULE__} . . . ")
Logger.info("Running #{__MODULE__} . . . ")
Supervisor.PyOperatorManager.launch([], "baz", "main")
|> Foo.main()
{:noreply, state}
end
end
defmodule Supervisor.PyOperatorManager do
use Supervisor
@timeout 60_000
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: 15,
max_overflow: 20
)
]
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}, @timeout)
end,
@timeout
)
end
end
defmodule Server.PyOperator do
use GenServer
use Export.Python
require Logger
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({args, py_module, py_lambda}, _from, %{py: py} = state) do
results = Python.call(py, py_module, py_lambda, [args])
{outcome, output} = results
{outcome, Jason.decode!(output)}
|> LogBook.write_to_log(__MODULE__, 26)
{:reply, results, state}
end
@impl true
def terminate(_reason, %{py: py}) do
Python.stop(py)
:ok
end
end
With this setup results come in every four or five seconds rather than every half-second. What am I missing?