Created
February 18, 2018 12:36
-
-
Save anildigital/c69e45d4a89abec57616cbd1e05b1102 to your computer and use it in GitHub Desktop.
Simple Example for Dynamic Supervisor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Worker1 do | |
def start_link() do | |
Task.start_link(fn -> | |
Stream.repeatedly(fn -> :rand.uniform(1000) end) | |
|> Stream.each(&:timer.sleep/1) | |
|> Stream.each(fn _ -> IO.puts("worker 1") end) | |
|> Stream.run() | |
end) | |
end | |
end | |
defmodule Worker2 do | |
def start_link() do | |
Task.start_link(fn -> | |
Stream.repeatedly(fn -> :rand.uniform(1000) end) | |
|> Stream.each(&:timer.sleep/1) | |
|> Stream.each(fn _ -> IO.puts("worker 2") end) | |
|> Stream.run() | |
end) | |
end | |
end | |
options = [ | |
name: Dyn.Supervisor, | |
strategy: :one_for_one | |
] | |
DynamicSupervisor.start_link(options) | |
DynamicSupervisor.start_child(Dyn.Supervisor, %{ | |
id: Worker1, | |
start: {Worker1, :start_link, []} | |
}) | |
DynamicSupervisor.start_child(Dyn.Supervisor, %{ | |
id: Worker2, | |
start: {Worker2, :start_link, []} | |
}) | |
:timer.sleep(:infinity) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment