Last active
March 24, 2018 13:37
-
-
Save suhanlee/504d37d470e17760301df1d7afd37587 to your computer and use it in GitHub Desktop.
process_bottleneck.ex
This file contains hidden or 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
server = Server.start | |
Enum.each(1..5, fn(i) -> | |
spawn(fn -> | |
IO.puts "Sending msg ##{i}" | |
response = Server.send_msg(server, i) | |
IO.puts "Response: #{response}" | |
end) | |
end) |
This file contains hidden or 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
def loop | |
receive | |
{:message, msg} -> | |
do_something(msg) | |
other -> | |
log_unkown_message(other) | |
end | |
loop | |
end |
This file contains hidden or 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 Server do | |
def start do | |
spawn(fn -> loop end) | |
end | |
def send_msg(server, message) do | |
send(server, {self, message}) | |
receive do | |
{:response, response} -> response | |
end | |
end | |
defp loop do | |
receive do | |
{caller, msg} -> | |
:timer.sleep(1000) | |
send(caller, {:response, msg}) | |
end | |
loop | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment