Created
May 26, 2017 07:53
-
-
Save sasa1977/9b99753d0479992705fc6933784910b5 to your computer and use it in GitHub Desktop.
Simple one for one supervising multiple children
This file contains hidden or 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 | |
| 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