Created
July 23, 2018 02:13
-
-
Save Zambito1/ff69be5ecb178c56c1e660a4efa2a441 to your computer and use it in GitHub Desktop.
Demonstration of multiple receive blocks in a single Elixir process
This file contains 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 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