Skip to content

Instantly share code, notes, and snippets.

@trestrantham
Last active August 29, 2015 14:04
Show Gist options
  • Save trestrantham/d9da472970e5623b50db to your computer and use it in GitHub Desktop.
Save trestrantham/d9da472970e5623b50db to your computer and use it in GitHub Desktop.
{: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