Editing
Supervisor (Erlang)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Supervisor (Erlang) == [[File:Elixir_supervisor.png|thumb|right|300px|Supervisor in Elixir]] A **Supervisor** in **Erlang** is a behavior that helps in building fault-tolerant systems by monitoring and controlling a set of child processes. It is one of the key components of **OTP (Open Telecom Platform)**, which is widely used in Elixir for building concurrent and reliable applications. === Functionality === A Supervisor manages a group of child processes, which are typically workers responsible for performing different tasks. The main functionality of a Supervisor can be summarized as follows: * **Starting Child Processes**: A Supervisor starts and initializes the specified child processes when it starts up. * **Monitoring Child Processes**: A Supervisor monitors the state of its child processes, detecting and responding to failures. * **Restarting Child Processes**: In case of a child process failure, a Supervisor is responsible for restarting the failed process. * **Managing Child Process Dependencies**: A Supervisor can define dependencies between child processes, ensuring proper startup and shutdown order. === Supervision Strategies === Supervisors support different strategies for handling child process failures. The most commonly used strategies are: * **One for One**: In this strategy, when a child process fails, only that specific child process is restarted. * **One for All**: If any child process fails, all the child processes supervised by the Supervisor are restarted. * **Rest for One**: If a child process fails, the Supervisor restarts that specific child process and all the processes that were started after it. === Supervision Trees === Supervisors can be organized in a hierarchical manner, forming a "supervision tree." This helps in creating a fault-tolerant system where failures can be isolated and handled in a structured way. A supervision tree typically consists of a top-level Supervisor, which supervises multiple intermediate Supervisors, which in turn supervise worker processes. === Usage in Elixir === In Elixir, the Supervisor behavior is implemented through the `Supervisor` module in the `:simple_one_for_one` and `:one_for_all` strategies. Elixir provides a clean and expressive syntax for defining Supervisors using the `use Supervisor` macro. Here's an example: ```elixir defmodule MySupervisor do use Supervisor def start_link do Supervisor.start_link(__MODULE__, :ok, name: __MODULE__) end def init(:ok) do children = [ worker(MyWorker, []), supervisor(MyOtherSupervisor, []) ] supervise(children, strategy: :one_for_one) end end ``` In this example, `MySupervisor` is defined as a Supervisor module, managing a child worker process `MyWorker` and another Supervisor `MyOtherSupervisor`. === Summary === Supervisors play a critical role in developing robust and fault-tolerant systems in Elixir. They provide the foundation for creating supervision trees and managing the lifecycle of child processes. Understanding how to use Supervisors effectively is essential for building reliable Elixir applications. == See Also == * [[OTP (Open Telecom Platform)]] * [[Concurrency (Elixir)]] * [[Fault Tolerance (Elixir)]] * [[GenServer (Elixir)]] [[Category: Elixir (programming language)]] [[Category: OTP]]
Summary:
Please note that all contributions to Elixir Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Elixir Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information