Skip to content

Instantly share code, notes, and snippets.

@sasa1977
Created May 26, 2017 07:53
Show Gist options
  • Select an option

  • Save sasa1977/9b99753d0479992705fc6933784910b5 to your computer and use it in GitHub Desktop.

Select an option

Save sasa1977/9b99753d0479992705fc6933784910b5 to your computer and use it in GitHub Desktop.
Simple one for one supervising multiple children
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
import Supervisor.Spec
{:ok, sup} =
Supervisor.start_link(
[worker(Kernel, [], restart: :temporary, function: :apply)],
strategy: :simple_one_for_one
)
Supervisor.start_child(sup, [Worker1, :start_link, []])
Supervisor.start_child(sup, [Worker2, :start_link, []])
:timer.sleep(:infinity)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment