Skip to content

Instantly share code, notes, and snippets.

@rwdaigle
Last active August 29, 2015 14:23
Show Gist options
  • Save rwdaigle/6e1fa4882975f5171a20 to your computer and use it in GitHub Desktop.
Save rwdaigle/6e1fa4882975f5171a20 to your computer and use it in GitHub Desktop.
Parallelized collection processing w/ sub-process supervision
alias Task.Supervisor
{:ok, supervisor} = Supervisor.start_link
double = fn(n) -> n*2 end
[1, 2, 3]
|> Enum.map(&Supervisor.async(supervisor, fn -> double.(&1) end))
|> Enum.map(&Task.await/1)
#=> [2, 4, 6]
[1, 2, "ruhroh"]
|> Enum.map(&Supervisor.async(supervisor, fn -> double.(&1) end))
|> Enum.map(&Task.await/1)
# 10:56:29.550 [error] GenServer #PID<0.63.0> terminating
# Last message: {:EXIT, #PID<0.58.0>, {:badarith, [{:erlang, :*, ["ruhroh", 2], []}, {Task.Supervised, :do_apply, 2, [file: 'lib/task/supervised.ex', line: 74]}, {Task.Supervised, :reply, 3, [file: 'lib/task/supervised.ex', line: 50]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 237]}]}}
@rwdaigle
Copy link
Author

Follow up Elixir homework to https://github.com/elixir-lang/ex_doc/pull/227/files.

Thanks to @peregrine for the nudge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment