Skip to content

Commit

Permalink
feat(Poolboy): implement parallel processing for Squarer.square
Browse files Browse the repository at this point in the history
Another function has been added that will spawn processes for each
number in a given range and push each number in the list to the Poolboy
worker for processing.

As these spawned processes won't return anything back to the main
application, the Worker has been updated to print out the current
background task that it is running.

There is an issue with this commit that Poolboy will timeout if it
cannot allocated a worker within the default time of 5 seconds and as
soon as it times out it will no longer take on any new jobs.
  • Loading branch information
Harvey Ball authored and thestonefox committed Nov 24, 2015
1 parent 341e209 commit 6d94ccd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/elixir_poolboy_example.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ defmodule ElixirPoolboyExample do
pool_square(x)
end

def parallel_pool(range) do
Enum.each(
range,
fn(x) -> spawn( fn() -> pool_square(x) end ) end
)
end

defp pool_square(x) do
:poolboy.transaction(
pool_name(),
Expand Down
1 change: 1 addition & 0 deletions lib/elixir_poolboy_example/worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule ElixirPoolboyExample.Worker do

def handle_call(data, from, state) do
result = ElixirPoolboyExample.Squarer.square(data)
IO.puts "Worker Reports: #{data} * #{data} = #{result}"
{:reply, [result], state}
end
end

0 comments on commit 6d94ccd

Please sign in to comment.