Skip to content

Instantly share code, notes, and snippets.

@micahboyd
Created May 18, 2018 04:46
Show Gist options
  • Select an option

  • Save micahboyd/29ebd2bad0f167d1904b890edc9d4979 to your computer and use it in GitHub Desktop.

Select an option

Save micahboyd/29ebd2bad0f167d1904b890edc9d4979 to your computer and use it in GitHub Desktop.
Elixir Ping Pong
defmodule PingPong do
def start do
ping_process = spawn(Ping, :loop, [])
pong_process = spawn(Pong, :loop, [])
send(ping_process, {:pong, pong_process})
end
def ping do
Ping
end
end
defmodule Ping do
def loop(n \\ []) do
receive do
{:pong, sender_pid} ->
:timer.sleep(1_000)
send(sender_pid, {:ping, self()})
IO.puts("ping ----")
loop()
_ ->
loop()
end
end
end
defmodule Pong do
def loop(n \\ []) do
receive do
{:ping, sender_pid} ->
:timer.sleep(1_000)
send(sender_pid, {:pong, self()})
IO.puts("---- pong")
loop()
_ ->
loop()
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment