Skip to content

Instantly share code, notes, and snippets.

@anildigital
Created February 18, 2018 12:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anildigital/c69e45d4a89abec57616cbd1e05b1102 to your computer and use it in GitHub Desktop.
Save anildigital/c69e45d4a89abec57616cbd1e05b1102 to your computer and use it in GitHub Desktop.
Simple Example for Dynamic Supervisor
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