Last active
February 12, 2019 21:47
-
-
Save cprieto/44c6569531762f9aa8f4083d6cfc5cf6 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
defmodule SpawnEx1 do | |
import :timer, only: [sleep: 1] | |
def greet(pid) do | |
send pid, {:ok, "I'm alive"} | |
exit(:dead) | |
end | |
def process_messages() do | |
receive do | |
{:ok, message} -> | |
IO.puts "I got #{message}" | |
process_messages() | |
end | |
end | |
def run do | |
me = self() | |
spawn_link(SpawnEx1, :greet, [me]) | |
sleep 500 | |
process_messages() | |
end | |
end | |
SpawnEx1.run |
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 SpawnEx2 do | |
import :timer, only: [sleep: 1] | |
def greet(pid) do | |
send pid, {:ok, "I'm alive"} | |
raise "I am dead" | |
end | |
def process_messages() do | |
receive do | |
{:ok, message} -> | |
IO.puts "I got #{message}" | |
process_messages() | |
end | |
end | |
def run do | |
me = self() | |
spawn_link(SpawnEx2, :greet, [me]) | |
sleep 500 | |
process_messages() | |
end | |
end | |
SpawnEx2.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment