defmodule Server.Foo 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(state) do
Process.flag(:trap_exit, true)
Logger.info("Launching #{__MODULE__} . . . ")
priv_path = Path.join(:code.priv_dir(:arbit), "python")
{:ok, py} = Python.start_link(python_path: priv_path)
fetch(py)
|> Bar.main()
Process.send_after(self(), :tick, @ms_sleep_interval)
{:ok, Map.put(state, :py, py)}
end
def fetch(py) do
Logger.info("Running #{__MODULE__} . . . ")
Python.call(py, "baz", "main", [])
end
@impl true
def handle_info(:tick, %{py: py} = state) do
Process.send_after(self(), :tick, @ms_sleep_interval)
fetch(py)
|> Bar.main()
{:noreply, state}
end
def handle_info({:EXIT, _, :normal}, %{py: py} = state) do
Process.send_after(self(), :tick, @ms_sleep_interval)
fetch(py)
|> Bar.main()
{:noreply, state}
end
@impl true
def terminate(_reason, %{py: py}) do
Python.stop(py)
:ok
end
end
Tick isn’t working as intended. A new instance is started every few seconds, rather than every half-second. What am I overlooking?