Created
January 16, 2019 01:31
-
-
Save yowchun93/05f151f1add138bf26c6c5a574652d46 to your computer and use it in GitHub Desktop.
Elixir Processes
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
run_query = fn(query_def) -> | |
:timer.sleep(2000) | |
"#{query_def} result" | |
end | |
#after 2 seconds -> "query 1 result" | |
run_query.("query 1") | |
# after 10 seconds result returns | |
1..5 |> Enum.map(&run_query.("query #{&1}")) | |
## spawning processes, returns a PID | |
spawn( fn -> IO.puts(run_query.("query_1")) end) | |
# async query | |
async_query = fn ( query_def) -> | |
spawn( fn -> IO.puts(run_query.(query_def)) end) | |
end | |
# spawning 5 processes | |
1..5 |> Enum.map(&async_query.("query #{&1}")) | |
Enum.each(1..5, &async_query.("query #{&1}")) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment