Task.Supervisor

Task.Supervisor

A task supervisor.

This module defines a supervisor which can be used to dynamically supervise tasks. Behind the scenes, this module is implemented as a :simple_one_for_one supervisor where the workers are temporary (i.e. they are not restarted after they die).

See the Task module for more information.

Name Registration

A Task.Supervisor is bound to the same name registration rules as a GenServer. Read more about them in the GenServer docs.

Summary

Functions

async(supervisor, fun)

Starts a task that can be awaited on

async(supervisor, module, fun, args)

Starts a task that can be awaited on

async_nolink(supervisor, fun)

Starts a task that can be awaited on

async_nolink(supervisor, module, fun, args)

Starts a task that can be awaited on

children(supervisor)

Returns all children pids

start_child(supervisor, fun)

Starts a task as a child of the given supervisor

start_child(supervisor, module, fun, args)

Starts a task as a child of the given supervisor

start_link(opts \\ [])

Starts a new supervisor

terminate_child(supervisor, pid)

Terminates the child with the given pid

Functions

async(supervisor, fun)

async(Supervisor.supervisor, (... -> any)) :: Task.t

Starts a task that can be awaited on.

The supervisor must be a reference as defined in Task.Supervisor. The task will still be linked to the caller, see Task.async/3 for more information and async_nolink/2 for a non-linked variant.

async(supervisor, module, fun, args)

async(Supervisor.supervisor, module, atom, [term]) :: Task.t

Starts a task that can be awaited on.

The supervisor must be a reference as defined in Task.Supervisor. The task will still be linked to the caller, see Task.async/3 for more information and async_nolink/2 for a non-linked variant.

async_nolink(supervisor, fun)

async_nolink(Supervisor.supervisor, (... -> any)) :: Task.t

Starts a task that can be awaited on.

The supervisor must be a reference as defined in Task.Supervisor. The task won’t be linked to the caller, see Task.async/3 for more information.

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 handle_info 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.

async_nolink(supervisor, module, fun, args)

async_nolink(Supervisor.supervisor, module, atom, [term]) :: Task.t

Starts a task that can be awaited on.

The supervisor must be a reference as defined in Task.Supervisor. The task won’t be linked to the caller, see Task.async/3 for more information.

children(supervisor)

children(Supervisor.supervisor) :: [pid]

Returns all children pids.

start_child(supervisor, fun)

start_child(Supervisor.supervisor, (... -> any)) :: {:ok, pid}

Starts a task as a child of the given supervisor.

Note that the spawned process is not linked to the caller, but only to the supervisor. This command is useful in case the task needs to perform side-effects (like I/O) and does not need to report back to the caller.

start_child(supervisor, module, fun, args)

start_child(Supervisor.supervisor, module, atom, [term]) :: {:ok, pid}

Starts a task as a child of the given supervisor.

Similar to start_child/2 except the task is specified by the given module, fun and args.

start_link(opts \\ [])

start_link(Supervisor.options) :: Supervisor.on_start

Starts a new supervisor.

The supported options are:

  • :name - used to register a supervisor name, the supported values are described under the Name Registration section in the GenServer module docs;

  • :restart - the restart strategy, may be :temporary (the default), :transient or :permanent. Check Supervisor.Spec for more info. Defaults to :temporary so tasks aren’t automatically restarted when they complete nor in case of crashes;

  • :shutdown - :brutal_kill if the tasks must be killed directly on shutdown or an integer indicating the timeout value, defaults to 5000 milliseconds;

  • :max_restarts and :max_seconds - as specified in Supervisor.Spec.supervise/2;

terminate_child(supervisor, pid)

terminate_child(Supervisor.supervisor, pid) :: :ok

Terminates the child with the given pid.

© 2012–2017 Plataformatec
Licensed under the Apache License, Version 2.0.
https://hexdocs.pm/elixir/1.3.4/Task.Supervisor.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部