task_supervisor (ex_stdlib v0.3.0)

View Source

A supervisor for dynamically spawned tasks, inspired by Elixir's Task.Supervisor.

A task supervisor is a dynamic_supervisor whose children are tasks. Tasks started with async/2 and async_nolink/2 return a task that works with the functions of the task module (await/2, yield/2, shutdown/2...).

   {ok, Sup} = task_supervisor:start_link([{name, my_task_sup}]),
  
   %% Fire and forget
   {ok, _Pid} = task_supervisor:start_child(my_task_sup, fun() -> do_work() end),
  
   %% Not linked to the caller: a crash in the task does not take it down
   Task = task_supervisor:async_nolink(my_task_sup, fun() -> fetch() end),
   case task:yield(Task, 1000) of
       {ok, Result} -> Result;
       {exit, Reason} -> {error, Reason};
       nil -> task:shutdown(Task), {error, timeout}
   end.

Like task:async/1, an exception raised by the task function is sent back to the caller: await re-raises it and yield returns {exit, Reason}. If the task process itself exits, the caller of an async/2 task exits too (it is linked), while the caller of an async_nolink/2 task sees the exit through await/yield.

Summary

Functions

Starts a task under the supervisor that must be awaited on, with default options.

Starts a task under the supervisor that must be awaited on.

Like async/3 with Module:Function(Args...) and default options.

Like async/3 with Module:Function(Args...).

Starts a task under the supervisor that can be awaited on, but is not linked to the caller, with default options.

Starts a task under the supervisor that can be awaited on, but is not linked to the caller.

Like async_nolink/3 with Module:Function(Args...) and default options.

Like async_nolink/3 with Module:Function(Args...).

Runs Fun concurrently on each element of List under the supervisor, with default options. See task:async_stream/3.

Runs Fun concurrently on each element of List under the supervisor. The tasks are linked to the caller.

Like async_stream/4 with Module:Function(Elem, Args...) and default options.

Like async_stream/4 with Module:Function(Elem, Args...).

Like async_stream/3, but the tasks are not linked to the caller.

Like async_stream/4, but the tasks are not linked to the caller, so a task process that exits is reported as {exit, Reason}.

Like async_stream_nolink/4 with Module:Function(Elem, Args...) and default options.

Like async_stream_nolink/4 with Module:Function(Elem, Args...).

Returns a child specification to start a task supervisor under a supervisor, with the same options as start_link/1.

Returns the pids of all tasks running under the supervisor.

Starts a task as a child of the supervisor, with default options.

Starts a task as a child of the supervisor. The task is not linked to the caller and its result is discarded.

Like start_child/3 with Module:Function(Args...) and default options.

Like start_child/3 with Module:Function(Args...).

Starts an unnamed task supervisor.

Starts a task supervisor linked to the caller.

Stops the supervisor, shutting down all its tasks.

Terminates the task with the given pid.

Types

async_option/0

-type async_option() :: {shutdown, brutal_kill | timeout()}.

child_option/0

-type child_option() ::
          {restart, permanent | transient | temporary} | {shutdown, brutal_kill | timeout()}.

start_option/0

-type start_option() ::
          {name, atom() | {local, atom()} | {global, term()} | {via, module(), term()}} |
          {max_restarts, non_neg_integer()} |
          {max_seconds, pos_integer()} |
          {max_children, non_neg_integer() | infinity}.

supervisor/0

-type supervisor() :: pid() | atom() | {atom(), node()} | {global, term()} | {via, module(), term()}.

Functions

async(Sup, Fun)

-spec async(supervisor(), fun(() -> term())) -> task:task().

Starts a task under the supervisor that must be awaited on, with default options.

async(Sup, Fun, Opts)

-spec async(supervisor(), fun(() -> term()), [async_option()]) -> task:task().

Starts a task under the supervisor that must be awaited on.

The task is linked to and monitored by the caller, as with task:async/1. Accepts the shutdown option (default 5000).

async(Sup, Module, Function, Args)

-spec async(supervisor(), module(), atom(), [term()]) -> task:task().

Like async/3 with Module:Function(Args...) and default options.

async(Sup, Module, Function, Args, Opts)

-spec async(supervisor(), module(), atom(), [term()], [async_option()]) -> task:task().

Like async/3 with Module:Function(Args...).

async_nolink(Sup, Fun)

-spec async_nolink(supervisor(), fun(() -> term())) -> task:task().

Starts a task under the supervisor that can be awaited on, but is not linked to the caller, with default options.

async_nolink(Sup, Fun, Opts)

-spec async_nolink(supervisor(), fun(() -> term()), [async_option()]) -> task:task().

Starts a task under the supervisor that can be awaited on, but is not linked to the caller.

If the task process exits, await/2 exits and yield/2 returns {exit, Reason} instead of the caller crashing. Accepts the shutdown option (default 5000).

async_nolink(Sup, Module, Function, Args)

-spec async_nolink(supervisor(), module(), atom(), [term()]) -> task:task().

Like async_nolink/3 with Module:Function(Args...) and default options.

async_nolink(Sup, Module, Function, Args, Opts)

-spec async_nolink(supervisor(), module(), atom(), [term()], [async_option()]) -> task:task().

Like async_nolink/3 with Module:Function(Args...).

async_stream(Sup, List, Fun)

-spec async_stream(supervisor(), list(), fun((term()) -> term())) -> [{ok, term()} | {exit, term()}].

Runs Fun concurrently on each element of List under the supervisor, with default options. See task:async_stream/3.

async_stream(Sup, List, Fun, Opts)

-spec async_stream(supervisor(),
                   list(),
                   fun((term()) -> term()),
                   [task:stream_option() | async_option()]) ->
                      [{ok, term()} | {exit, term()}].

Runs Fun concurrently on each element of List under the supervisor. The tasks are linked to the caller.

Accepts the options of task:async_stream/3 plus shutdown.

async_stream(Sup, List, Module, Function, Args)

-spec async_stream(supervisor(), list(), module(), atom(), [term()]) -> [{ok, term()} | {exit, term()}].

Like async_stream/4 with Module:Function(Elem, Args...) and default options.

async_stream(Sup, List, Module, Function, Args, Opts)

-spec async_stream(supervisor(),
                   list(),
                   module(),
                   atom(),
                   [term()],
                   [task:stream_option() | async_option()]) ->
                      [{ok, term()} | {exit, term()}].

Like async_stream/4 with Module:Function(Elem, Args...).

async_stream_nolink(Sup, List, Fun)

-spec async_stream_nolink(supervisor(), list(), fun((term()) -> term())) ->
                             [{ok, term()} | {exit, term()}].

Like async_stream/3, but the tasks are not linked to the caller.

async_stream_nolink(Sup, List, Fun, Opts)

-spec async_stream_nolink(supervisor(),
                          list(),
                          fun((term()) -> term()),
                          [task:stream_option() | async_option()]) ->
                             [{ok, term()} | {exit, term()}].

Like async_stream/4, but the tasks are not linked to the caller, so a task process that exits is reported as {exit, Reason}.

async_stream_nolink(Sup, List, Module, Function, Args)

-spec async_stream_nolink(supervisor(), list(), module(), atom(), [term()]) ->
                             [{ok, term()} | {exit, term()}].

Like async_stream_nolink/4 with Module:Function(Elem, Args...) and default options.

async_stream_nolink(Sup, List, Module, Function, Args, Opts)

-spec async_stream_nolink(supervisor(),
                          list(),
                          module(),
                          atom(),
                          [term()],
                          [task:stream_option() | async_option()]) ->
                             [{ok, term()} | {exit, term()}].

Like async_stream_nolink/4 with Module:Function(Elem, Args...).

child_spec(Opts)

-spec child_spec([start_option()]) -> supervisor:child_spec().

Returns a child specification to start a task supervisor under a supervisor, with the same options as start_link/1.

children(Sup)

-spec children(supervisor()) -> [pid()].

Returns the pids of all tasks running under the supervisor.

start_child(Sup, Fun)

-spec start_child(supervisor(), fun(() -> term())) -> {ok, pid()} | {error, term()}.

Starts a task as a child of the supervisor, with default options.

start_child(Sup, Fun, Opts)

-spec start_child(supervisor(), fun(() -> term()), [child_option()]) -> {ok, pid()} | {error, term()}.

Starts a task as a child of the supervisor. The task is not linked to the caller and its result is discarded.

Options: restart (default temporary) and shutdown (default 5000). Returns {error, max_children} when the supervisor is full.

start_child(Sup, Module, Function, Args)

-spec start_child(supervisor(), module(), atom(), [term()]) -> {ok, pid()} | {error, term()}.

Like start_child/3 with Module:Function(Args...) and default options.

start_child(Sup, Module, Function, Args, Opts)

-spec start_child(supervisor(), module(), atom(), [term()], [child_option()]) ->
                     {ok, pid()} | {error, term()}.

Like start_child/3 with Module:Function(Args...).

start_link()

-spec start_link() -> {ok, pid()} | {error, term()}.

Starts an unnamed task supervisor.

start_link(Opts)

-spec start_link([start_option()]) -> {ok, pid()} | {error, term()}.

Starts a task supervisor linked to the caller.

Options: name (an atom or a {local, _}, {global, _} or {via, _, _} tuple), max_restarts (default 3), max_seconds (default 5) and max_children (default infinity).

stop(Sup)

-spec stop(supervisor()) -> ok.

Stops the supervisor, shutting down all its tasks.

terminate_child(Sup, Pid)

-spec terminate_child(supervisor(), pid()) -> ok | {error, not_found}.

Terminates the task with the given pid.