Last active
December 19, 2015 16:48
-
-
Save jschoch/5986443 to your computer and use it in GitHub Desktop.
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 Processes do | |
def werk(pid,list) do | |
pid <- {:ok,Enum.count list} | |
#exit(:normal) | |
#:timer.sleep(500) | |
#IO.puts("Done #{inspect self}") | |
end | |
def get_msg(pid) do | |
receive do | |
{:ok, value} when is_number(value) -> value | |
{:EXIT,pid,status} -> | |
IO.puts "#{inspect pid} #{status}" | |
0 | |
doh -> | |
IO.puts "Poof #{inspect doh}" | |
0 | |
after 10000 -> IO.puts "out of luck" | |
end | |
end | |
def go do | |
pids = Enum.map(1..10000,spawn_link(__MODULE__,:werk,[self,[1,2,3,4,5,6,7,8,9,10]])) | |
result = Enum.reduce pids,0,fn(pid,acc) -> get_msg(pid) + acc end | |
IO.puts result | |
end | |
def go2 do | |
IO.puts "Start #{inspect :calendar.now_to_datetime(:erlang.now)}" | |
pids = Enum.map(1..10000,fn(_) -> spawn_link(__MODULE__,:werk,[self,[1,2,3,4,5,6,7,8,9,10]]) end ) | |
list = get_msgs(pids,[]) | |
result = Enum.reduce list,0,fn(e,acc) -> e + acc end | |
IO.puts result | |
IO.puts "Stop #{inspect :calendar.now_to_datetime(:erlang.now)}" | |
end | |
def get_msgs([],list) do | |
list | |
end | |
def get_msgs(pids,list) do | |
receive do | |
{:ok, value} when is_number(value) -> get_msgs(pids,[value|list]) | |
{:EXIT,pid,status} -> get_msgs(Enum.filter(pids,&1 != pid),list) | |
doh -> | |
IO.puts "Poof #{inspect doh}" | |
after 1000 -> IO.puts "out of luck" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment