Skip to content

Instantly share code, notes, and snippets.

@mahnve
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahnve/c3d08c1d35d6df8e8eeb to your computer and use it in GitHub Desktop.
Save mahnve/c3d08c1d35d6df8e8eeb to your computer and use it in GitHub Desktop.
Starts a few processes, who waits a randomly long time before calling back.
defmodule LatePrint do
def late_print(0) do
handle_message
end
def late_print(n) do
parent = self
spawn_link(fn ->
<< a :: 32, b :: 32, c :: 32 >> = :crypto.rand_bytes(12)
:random.seed(a,b,c)
r = :random.uniform(10000)
IO.puts(r)
:timer.sleep(r)
send parent, {:msg, "hello world from #{n}"}
end)
late_print(n-1)
end
def handle_message do
receive do
{:msg, contents} -> IO.puts contents
end
handle_message
end
end
LatePrint.late_print(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment