Last active
August 29, 2015 14:16
-
-
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.
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 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