Using supervisor with Task.async_stream_nolink?

I’ve been reading about Task.async_stream_nolink, which would iterate collection into heavy function using spawned process, which is what I want, as linked process would cause crashing all other tasks related to processing the collection.

The question is, how would I tell the supervisor not to spawn a new process when fails? or at least to decide to recover the process based on the state of failed process?

As sometimes, I don’t need to recover process after failed.

My supervisor looks like this:

defmodule MyApp.Supervisor do
  # Automatically defines child_spec/1
  use Supervisor

  def start_link(arg) do
    Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
  end

  def init(_arg) do
    Supervisor.init([
      {Stack, [:hello]}
    ], strategy: :one_for_one)
  end
end

Maybe You can use a simple_one_for_one strategy, with one of the following restart options…

  • temporary, never restarted
  • transient, restarted on abnormal termination

But where can I handle the failure of a spawned process?

With the restart option, supervisor will choose to restart, or not, the failing process. Isn’t it what You want to achieve?

I want to handle some logic, based on the failed process, so, how can I receive the process failure? shall I just use def handle_info({:EXIT, pid, :killed}, state) do ?

I am sorry, I do not remember which message You need to look at, if is it :EXIT, or :DOWN…

1 Like

Ok, thank you