Skip to content

Instantly share code, notes, and snippets.

@nathan-cruz77
Created July 27, 2018 17:31
Show Gist options
  • Save nathan-cruz77/0f4fc881495356cbb9c17c4832b14a08 to your computer and use it in GitHub Desktop.
Save nathan-cruz77/0f4fc881495356cbb9c17c4832b14a08 to your computer and use it in GitHub Desktop.
Async execution in elixir
defmodule Run do
def in_parallel(n) do
1..n
|> Enum.map(fn(i) ->
Task.async(fn -> :timer.sleep(i * 1_000) end)
end)
|> Enum.map(&Task.await/1)
end
def serial(n) do
Enum.map(1..n, fn(i) ->
:timer.sleep(i * 1_000)
end)
end
end
# It takes round 55s to run serially
Run.serial(10)
# Only 10s to run in parallel
Run.in_parallel(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment