Maxximiliann

Maxximiliann

Genserver/poolboy - How to relaunch a new instance of a process before it's done?

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?

Most Liked

al2o3cr

al2o3cr

PyOperatorManager.launch will block until the GenServer.call inside the poolboy transaction returns.

As a result, Server.Bar.handle_info will be blocked - if the call to launch takes too long then Process.send_after will enqueue a tick message 500ms later as expected, but the GenServer will not check its message queue until the work is done.

To prevent this you’ll need to change the structure of your code to ensure control returns from Server.Bar.handle_info promptly, and handle passing the results of launch to Foo.main separately.

al2o3cr

al2o3cr

The GenServer unblocks when the callback (handle_cast/handle_call/handle_info) returns control to the receive loop.

For instance, a handle_call can return {:noreply, state}, which leaves the sender of the call still waiting for a reply. Typical approaches for getting the reply back to the client use GenServer.reply either:

  • from a handle_info callback later, fetching the from tuple from the GenServer’s state
  • from some other process that’s been handed the from tuple

Here’s a longer example of the first kind (note the date - I didn’t see any obvious incompatibilities, but I didn’t run the code):

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement