Created
October 23, 2017 20:12
-
-
Save alexpeachey/508c692ca52a3ee7b8854db1d4e7520d to your computer and use it in GitHub Desktop.
Process communication with Elixir
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 PingPong do | |
def start do | |
ping_pid = spawn __MODULE__, :ping, [] | |
pong_pid = spawn __MODULE__, :pong, [] | |
send ping_pid, {pong_pid, :ping} | |
{ping_pid, pong_pid} | |
end | |
def ping do | |
receive do | |
{from, :ping} -> | |
IO.puts "PING!" | |
:timer.sleep 1000 | |
send from, {self(), :pong} | |
end | |
ping() | |
end | |
def pong do | |
receive do | |
{from, :pong} -> | |
IO.puts "PONG!" | |
:timer.sleep 1000 | |
send from, {self(), :ping} | |
end | |
pong() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment