Last active
August 29, 2015 14:04
-
-
Save trestrantham/d9da472970e5623b50db to your computer and use it in GitHub Desktop.
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
{:ok, manager} = GenEvent.start_link | |
stream = GenEvent.stream(manager, [mode: :async]) | |
pid1 = spawn_link fn -> | |
for x <- stream do | |
IO.puts "pid1 -> sleeping for 1 second then inspecting" | |
:timer.sleep(1000) | |
IO.inspect(x) | |
end | |
end | |
pid2 = spawn_link fn -> | |
for x <- stream do | |
IO.puts "pid2 -> sleeping for 2 seconds then inspecting" | |
:timer.sleep(2000) | |
IO.inspect(x) | |
end | |
end | |
iex> for x <- [1,2,3,4,5], do: GenEvent.notify(manager, x) | |
[:ok, :ok, :ok, :ok, :ok] | |
pid2 -> sleeping for 2 seconds then inspecting | |
1 | |
pid1 -> sleeping for 1 second then inspecting | |
1 | |
pid2 -> sleeping for 2 seconds then inspecting | |
2 | |
pid1 -> sleeping for 1 second then inspecting | |
2 | |
pid2 -> sleeping for 2 seconds then inspecting | |
3 | |
pid1 -> sleeping for 1 second then inspecting | |
3 | |
pid2 -> sleeping for 2 seconds then inspecting | |
4 | |
pid1 -> sleeping for 1 second then inspecting | |
4 | |
pid2 -> sleeping for 2 seconds then inspecting | |
5 | |
pid1 -> sleeping for 1 second then inspecting | |
5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment