Created
July 27, 2018 17:31
-
-
Save nathan-cruz77/0f4fc881495356cbb9c17c4832b14a08 to your computer and use it in GitHub Desktop.
Async execution in elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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