Skip to content

Instantly share code, notes, and snippets.

@Zambito1
Created July 23, 2018 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zambito1/ff69be5ecb178c56c1e660a4efa2a441 to your computer and use it in GitHub Desktop.
Save Zambito1/ff69be5ecb178c56c1e660a4efa2a441 to your computer and use it in GitHub Desktop.
Demonstration of multiple receive blocks in a single Elixir process
defmodule MultipleReceive do
def myProc do
IO.puts "Func starting: "
receive do
:first -> IO.puts "Received first message!"
end
receive do
:second -> IO.puts "Received second message!"
end
receive do
:last -> IO.puts "Wow, everything was received in the right order!"
after
1_000 -> "The last message was lost :("
end
IO.puts "Func terminating..."
end
def main do
IO.puts "Starting main"
pid = spawn(fn -> myProc end)
IO.puts "Tricky value, we want to handle this last!"
send(pid, :last)
IO.puts "Sending first message..."
send(pid, :first)
IO.puts "Sending second message..."
send(pid, :second)
end
end
MultipleReceive.main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment