Editing
Supervisor module
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 module == The **Supervisor module** is a key component in the **Elixir** programming language. It plays a crucial role in **Elixir's concurrency model** by supervising and managing **child processes**. In this article, we will explore the functionality and usage of the Supervisor module in detail. == Overview == The Supervisor module is part of the **Elixir standard library** and provides a **supervision behavior** for building **fault-tolerant systems**. It allows developers to create and manage a **supervisor process**, which in turn monitors and controls a group of **child processes**. == Functionality == The Supervisor module offers several important functions and features: === Start a Supervisor === To start a supervisor, the `start_link/2` function is used. It takes a **module** implementing the **Supervisor behavior** and a **list of child specifications**. Each child specification describes a child process, including its **module**, **arguments**, and **options**. === Child Process Restart Strategies === The Supervisor module provides different **restart strategies** that define how child processes should be restarted in case of termination. These strategies include: * `:one_for_one`: Restart only the terminated child process. * `:one_for_all`: Restart all child processes. * `:rest_for_one`: Restart the terminated child process and all child processes started after it. * `:simple_one_for_one`: Restart only one specified child process. === Child Process Supervision === The Supervisor module monitors child processes and automatically restarts them based on the specified restart strategy. It ensures that the system remains **fault-tolerant** and keeps running even if individual processes fail. === Dynamic Children Management === Supervisors allow for **dynamic child management**. Child processes can be added or removed during runtime using functions such as `start_child/2`, `delete_child/2`, and `terminate_child/2`. == Supervisor Strategies == Supervisors can use different strategies to supervise child processes. These strategies define how the supervisor behaves when a child process terminates unexpectedly. Some common strategies include: * `:one_for_one`: Restart only the terminated child process. * `:one_for_all`: Restart all child processes. * `:rest_for_one`: Restart the terminated child process and all child processes started after it. * `:simple_one_for_one`: Restart only one specified child process. == Usage Examples == Let's take a look at a few examples that demonstrate the usage of the Supervisor module: === Example 1: Simple Supervisor === ```elixir defmodule MySupervisor do use Supervisor def start_link do Supervisor.start_link(__MODULE__, []) end @impl true def init(_) do children = [ worker(MyChild1, []), worker(MyChild2, []), worker(MyChild3, []) ] supervise(children, strategy: :one_for_one) end end ``` === Example 2: Dynamic Child Management === ```elixir defmodule DynamicSupervisor do use Supervisor def start_link do Supervisor.start_link(__MODULE__, []) end @impl true def init(_) do supervise_dynamic(strategy: :one_for_one, max_restarts: 3, max_seconds: 5) do [worker(MyDynamicChild, [])] end end end ``` == Conclusion == The Supervisor module is a powerful tool in the Elixir programming language for building fault-tolerant systems. By providing process supervision and automatic restarting of failed child processes, it ensures the stability and reliability of concurrent applications. Understanding the Supervisor module's functionality and correct usage is essential for developing robust Elixir applications. For more information, see the dedicated articles on **[Supervision Principles](Supervision_Principles)** and **[Child Specification](Child_Specification)**. {{Programming languages}} {{Elixir}}
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)
Templates used on this page:
Template:Elixir
(
edit
)
Template:Programming languages
(
edit
)
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