Editing
DynamicSupervisor
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!
== DynamicSupervisor == The `DynamicSupervisor` module in Elixir is a powerful tool that allows for dynamic supervision of processes. It is a part of the `Supervisor` behavior, which is key to building fault-tolerant systems in Elixir. With the `DynamicSupervisor`, you can dynamically add and remove child processes at runtime without needing to restart the supervision tree. = Usage = To use the `DynamicSupervisor`, you first need to start a dynamic supervisor by calling `DynamicSupervisor.start_link/2` or `DynamicSupervisor.start/2`. This will create a supervisor process that will handle the dynamic addition and removal of child processes. Once the dynamic supervisor is started, you can use the various functions available in the `DynamicSupervisor` module to manage the child processes. Some of the most commonly used functions include: * `DynamicSupervisor.start_child/2` to start a new child process * `DynamicSupervisor.terminate_child/2` to terminate a child process * `DynamicSupervisor.replace_child/3` to replace a child process with a new one * `DynamicSupervisor.update_child/3` to update the child specification of a running process = Example = Here is an example that demonstrates the usage of `DynamicSupervisor`: ```elixir defmodule MyApp.Supervisor do use DynamicSupervisor def start_link(_) do DynamicSupervisor.start_link(__MODULE__, nil, name: __MODULE__) end def init(nil) do DynamicSupervisor.init(strategy: :one_for_one) end end defmodule MyApp.Worker do def start_link(arg) do Task.start_link(fn -> loop(arg) end) end defp loop(counter) do IO.puts("Counter: #{counter}") :timer.sleep(1000) loop(counter + 1) end end {:ok, _} = MyApp.Supervisor.start_link(nil) {:ok, _} = DynamicSupervisor.start_child(MyApp.Supervisor, {MyApp.Worker, :start_link, [0]}) :timer.sleep(5000) DynamicSupervisor.terminate_child(MyApp.Supervisor, worker_pid) ``` In this example, we define a dynamic supervisor using the `DynamicSupervisor` module. We then start the supervisor and add a child process using `DynamicSupervisor.start_child/2`. After a delay of 5 seconds, we terminate the child process. = Benefits = The `DynamicSupervisor` provides several benefits, including: * Dynamically adding and removing child processes without restarting the supervision tree * Increased flexibility and reusability of supervision trees * Simplified code and improved modularity = Conclusion = The `DynamicSupervisor` module in Elixir is a powerful tool for dynamic process supervision. It allows for the dynamic addition and removal of child processes at runtime without needing to restart the supervision tree. By using the `DynamicSupervisor`, you can build fault-tolerant systems that can adapt and evolve as needed. == See Also == * [[Supervisor]] * [[GenServer]] == References == <references /> [[Category:Elixir Programming Language]] [[Category:Supervision]] [[Category:Concurrency Control]]
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