Skip to content

Instantly share code, notes, and snippets.

@bsnux
Created October 13, 2025 20:43
Show Gist options
  • Select an option

  • Save bsnux/ee8ad8e06b5720b334ffd604f1a0be39 to your computer and use it in GitHub Desktop.

Select an option

Save bsnux/ee8ad8e06b5720b334ffd604f1a0be39 to your computer and use it in GitHub Desktop.
Concurrent requests in Elixir
#!/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