Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
defmodule ParallelRequests do
def execute_requests(urls) do
:inets.start()
Enum.map urls, do_parallel_request(&1, self)
do_collect_responses(length(urls), 0)
end
defp do_parallel_request(url, parent_id) do
spawn fn ->
response = :httpc.request url
parent_id <- { :response, response }
end
end
defp do_collect_responses(0, acc), do: acc
defp do_collect_responses(count, acc) do
receive do
{ :response, response } ->
{:ok, {_, _, body}} = response
do_collect_responses(count - 1, length(body) + acc)
end
end
def now do
{mega, sec, micro} = :erlang.now
mega * 1000000 * 1000000 + sec * 1000000 + micro
end
end
url = 'http://feeds.feedburner.com/NoticiasINFO-Ciencia?format=xml'
urls = Enum.map(1..1000, fn(_) -> url end)
start = ParallelRequests.now
size = ParallelRequests.execute_requests(urls)
finish = ParallelRequests.now
delta = (finish - start) / 1000 / 1000
IO.puts "All requests size: #{size}"
IO.puts "Time elapsed: #{delta} seconds"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.