Despite, there’s not much projects in Elixir nowadays developed in Ukraine, we believe that Elixir’s features will make it very popular soon. That’s why we seriously considering the practical use of Elixir for JS developers and for Rubyists. One of the main reasons for that is Elixir concurrency. And today we are going to explore with you the Elixir Concurrency example and put it in practice. Are you ready?
We aim to download concurrently different prices and return a map with the latest prices. Let’s see this process step by step.
- Define a function
ticker_url(product_id)to return the URL string for the given product.
get_pricefunction using only our HTTPClient module instead of HTTPoison and Jason.
- Focus on making one single concurrent request, then scale to multiple requests.
- Have everything to write a compact concurrent
get_prices(products)function, using Elixir pipes.
- The first
spawn_and_send_price(product_id)for each product, returning a list of pids.
- The second
Enum.maptakes the list of pids as input, running
await(pid)for each pid. It then returns the list of results.
Enum.intotransforms the list of tuples
, in a Map.
Try to get the prices multiple times and find more examples here.
Que is a job processing library in Elixir that we explored closer and we think it can be interesting to you too.