Skip to content

Instantly share code, notes, and snippets.

@jduan
Created May 19, 2016 23:20
Show Gist options
  • Save jduan/e97cd6746e97fe4fd67e8cc892d216e9 to your computer and use it in GitHub Desktop.
Save jduan/e97cd6746e97fe4fd67e8cc892d216e9 to your computer and use it in GitHub Desktop.
defmodule Parallel do
# Allows mapping over a collection using N parallel processes
def pmap(collection, function) do
# Get this process's PID
me = self
collection
|>
Enum.map(fn (elem) ->
# For each element in the collection, spawn a process and
# tell it to:
# - Run the given function on that element
# - Call up the parent process
# - Send the parent its PID and its result
# Each call to spawn_link returns the child PID immediately.
spawn_link fn -> (send me, { self, function.(elem) }) end
end) |>
# Here we have the complete list of child PIDs. We don't yet know
# which, if any, have completed their work
Enum.map(fn (pid) ->
# For each child PID, in order, block until we receive an
# answer from that PID and return the answer
# While we're waiting on something from the first pid, we may
# get results from others, but we won't "get those out of the
# mailbox" until we finish with the first one.
receive do { ^pid, result } ->
IO.puts result
end
end)
end
end
HTTPoison.start
# Calculate squares of these numbers
parallelism = 20
Parallel.pmap(1..parallelism, fn(integer) ->
IO.puts "getting animal #{integer}"
# response = HTTPoison.get!("http://localhost:8888/animals/#{integer}", [], [recv_timeout: 1000000])
response = HTTPoison.get!("http://localhost:8888/hello?name=#{integer}", [], [recv_timeout: 1000000])
IO.puts "response for #{integer}: #{response.body}"
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment