Skip to content

Instantly share code, notes, and snippets.

@yowchun93
Created January 16, 2019 01:31
Show Gist options
  • Save yowchun93/05f151f1add138bf26c6c5a574652d46 to your computer and use it in GitHub Desktop.
Save yowchun93/05f151f1add138bf26c6c5a574652d46 to your computer and use it in GitHub Desktop.
Elixir Processes
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