Skip to content

Instantly share code, notes, and snippets.

@alexpeachey
Created October 23, 2017 20:12
Show Gist options
  • Save alexpeachey/508c692ca52a3ee7b8854db1d4e7520d to your computer and use it in GitHub Desktop.
Save alexpeachey/508c692ca52a3ee7b8854db1d4e7520d to your computer and use it in GitHub Desktop.
Process communication with Elixir
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