Task.Supervisor.async_nolink
async_nolink
, go back to Task.Supervisor module for more information.
Specs
async_nolink(Supervisor.supervisor(), (() -> any()), Keyword.t()) :: Task.t()
Starts a task that can be awaited on.
The supervisor
must be a reference as defined in Supervisor
.
The task won't be linked to the caller, see Task.async/3
for
more information.
Raises an error if supervisor
has reached the maximum number of
children.
Options
:shutdown
-:brutal_kill
if the tasks must be killed directly on shutdown or an integer indicating the timeout value, defaults to 5000 milliseconds.
Compatibility with OTP behaviours
If you create a task using async_nolink
inside an OTP behaviour
like GenServer
, you should match on the message coming from the
task inside your GenServer.handle_info/2
callback.
The reply sent by the task will be in the format {ref, result}
,
where ref
is the monitor reference held by the task struct
and result
is the return value of the task function.
Keep in mind that, regardless of how the task created with async_nolink
terminates, the caller's process will always receive a :DOWN
message
with the same ref
value that is held by the task struct. If the task
terminates normally, the reason in the :DOWN
message will be :normal
.
Examples
Typically, you use async_nolink/3
when there is a reasonable expectation that
the task may fail, and you don't want it to take down the caller. Let's see an
example where a GenServer
is meant to run a single task and track its status:
defmodule MyApp.Server do
use GenServer
# ...
def start_task do
GenServer.call(__MODULE__, :start_task)
end
# In this case the task is already running, so we just return :ok.
def handle_call(:start_task, _from, %{ref: ref} = state) when is_reference(ref) do
{:reply, :ok, state}
end
# The task is not running yet, so let's start it.
def handle_call(:start_task, _from, %{ref: nil} = state) do
task =
Task.Supervisor.async_nolink(MyApp.TaskSupervisor, fn ->
...
end)
# We return :ok and the server will continue running
{:reply, :ok, %{state | ref: task.ref}}
end
# The task completed successfully
def handle_info({ref, answer}, %{ref: ref} = state) do
# We don't care about the DOWN message now, so let's demonitor and flush it
Process.demonitor(ref, [:flush])
# Do something with the result and then return
{:noreply, %{state | ref: nil}}
end
# The task failed
def handle_info({:DOWN, ref, :process, _pid, _reason}, %{ref: ref} = state) do
# Log and possibly restart the task...
{:noreply, %{state | ref: nil}}
end
end
Specs
Starts a task that can be awaited on.
The supervisor
must be a reference as defined in Supervisor
.
The task won't be linked to the caller, see Task.async/3
for
more information.
Raises an error if supervisor
has reached the maximum number of
children.
Note this function requires the task supervisor to have :temporary
as the :restart
option (the default), as async_nolink/4
keeps a
direct reference to the task which is lost if the task is restarted.