Created
October 13, 2025 20:43
-
-
Save bsnux/ee8ad8e06b5720b334ffd604f1a0be39 to your computer and use it in GitHub Desktop.
Concurrent requests in Elixir
This file contains hidden or 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
| #!/usr/bin/env elixir | |
| # | |
| # Simple example for concurrent request in Elixir | |
| # | |
| Mix.install([ | |
| {:req, "~> 0.5"} | |
| ]) | |
| urls = for id <- 1..100, do: "https://jsonplaceholder.typicode.com/posts/#{id}" | |
| t_start = System.monotonic_time() | |
| urls | |
| |> Task.async_stream( | |
| fn url -> Req.get!(url).body["title"] end, | |
| max_concurrency: 10, | |
| timeout: 5_000 | |
| ) | |
| |> Enum.each(fn {:ok, title} -> IO.inspect(title) end) | |
| t_end = System.monotonic_time() | |
| elapsed_ms = System.convert_time_unit(t_end - t_start, :native, :millisecond) | |
| IO.puts("Fetched in #{elapsed_ms} ms") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment